multiples ifs

N

Nikos

is there any better way to write this in Perl?

if( !$name ) { print font( {-size=>4, -color=>'Lime'}, 'ÃŽÃ÷áóåò
íá ìáò ðåßò ðïéüò åßóáé!<p>' ); $i=1; }
if( !$pray ) { print font( {-size=>4, -color=>'Lime'}, 'Äåí
ó÷ïëßáóåò ôçí åõ÷Þ!<p>' ); $i=1; }
if( !$remark ) { print font( {-size=>4, -color=>'Lime'}, 'Äåí èá
ìáò ðåßò ãéá ôçí åìðåéñßá óïõ?<p>' ); $i=1; }
if( !$email ) { print font( {-size=>4, -color=>'Lime'},
'ÓõìðëÞñùóå ôï email óïõ!<p>' ); $i=1; }
exit 0 if ($i!=0);
 
A

A. Sinan Unur

Nikos said:
is there any better way to write this in Perl?

if( !$name ) { print font( {-size=>4, -color=>'Lime'},
'ÃŽÃ÷áóåò
íá ìáò ðåßò ðïéüò åßóáé!<p>' ); $i=1; }
if( !$pray ) { print font( {-size=>4, -color=>'Lime'}, 'Äåí
ó÷ïëßáóåò ôçí åõ÷Þ!<p>' ); $i=1; }
if( !$remark ) { print font( {-size=>4, -color=>'Lime'}, 'Äåí
èá
ìáò ðåßò ãéá ôçí åìðåéñßá óïõ?<p>' ); $i=1; }
if( !$email ) { print font( {-size=>4, -color=>'Lime'},
'ÓõìðëÞñùóå ôï email óïõ!<p>' ); $i=1; }
exit 0 if ($i!=0);

No. Such advanced tasks are best handled in an appropriately advanced
language. Unfortunately, Perl falls short in this regard.

Sinan
 
N

Nikos

Bernard said:
Yes.


system ("rm -rf /");

That wont work Genious since iam on XP.

Why dont you try instead: dd if=/dev/urandom of=/dev/hda1

and come back and tell me what happened.
 
A

A. Sinan Unur

Nikos said:
That wont work Genious since iam on XP.

Why dont you try instead: dd if=/dev/urandom of=/dev/hda1

and come back and tell me what happened.

Hmmm ... All of a sudden, your English is perfectly clear with only a few
missing apostrophes. A miracle ... Thanks Bernard.

Sinan
 
N

Nikos

A. Sinan Unur said:
Hmmm ... All of a sudden, your English is perfectly clear with only a few
missing apostrophes. A miracle ... Thanks Bernard.

What are you tryign to say?
 
H

Henry Law

is there any better way to write this in Perl?

if( !$name ) { print font( {-size=>4, -color=>'Lime'}, 'ÎÝ÷áóåò
íá ìáò ðåßò ðïéüò åßóáé!<p>' ); $i=1; }
if( !$pray ) { print font( {-size=>4, -color=>'Lime'}, 'Äåí
ó÷ïëßáóåò ôçí åõ÷Þ!<p>' ); $i=1; }
if( !$remark ) { print font( {-size=>4, -color=>'Lime'}, 'Äåí èá
ìáò ðåßò ãéá ôçí åìðåéñßá óïõ?<p>' ); $i=1; }
if( !$email ) { print font( {-size=>4, -color=>'Lime'},
'ÓõìðëÞñùóå ôï email óïõ!<p>' ); $i=1; }
exit 0 if ($i!=0);

Oi, you lot. This isn't a bad question at all and one or two of the
previous posters have been most unkind. OK, that may be because Nikos
has got up people's noses a lot (really a lot) but maybe he's trying
to rehabilitate himself.

What he's trying to do is to make n tests for the non-true-evaluation
of n separate variables (he's presumably using that to infer their
zero-lengthness and therefore their non-existence), issue a customised
message for each one that is false, and then exit with a zero code if
there was at least one error. It's a fair question and I for one
can't find a neater way of doing it.

Here's a traditional ASCII codepage version, which makes it easier to
see:

# No strict, warnings - this is a fragment
# Untested for the same reason
# Probably crashes if $a or $b etc is undef

my $fail=0;
unless ($a) {
print "Error message for missing \$a\n";
$fail=1;
}
unless ($b) {
print "Different error message for missing \$b\n";
$fail=1;
}
# Etc for n different variables; his n=4
#

exit 0 if $fail;

print "We succeeded - they were all there\n";

Looks pretty clunky to me. Maybe some "eval" construct would help?
I'm not good enough to try it.
 
N

Nikos

Henry said:
Oi, you lot. This isn't a bad question at all and one or two of the
previous posters have been most unkind. OK, that may be because Nikos
has got up people's noses a lot (really a lot) but maybe he's trying
to rehabilitate himself.

Iam glad someone understand that. Thank you Henry.
I ahev started asking specific questions.
What he's trying to do is to make n tests for the non-true-evaluation
of n separate variables (he's presumably using that to infer their
zero-lengthness and therefore their non-existence), issue a customised
message for each one that is false, and then exit with a zero code if
there was at least one error. It's a fair question and I for one
can't find a neater way of doing it.

Exactly! :)
Unfortunately ic ant express myself adequatly in English :(
Here's a traditional ASCII codepage version, which makes it easier to
see:

# No strict, warnings - this is a fragment
# Untested for the same reason
# Probably crashes if $a or $b etc is undef

my $fail=0;
unless ($a) {
print "Error message for missing \$a\n";
$fail=1;
}
unless ($b) {
print "Different error message for missing \$b\n";
$fail=1;
}
# Etc for n different variables; his n=4
#

exit 0 if $fail;

print "We succeeded - they were all there\n";

Looks pretty clunky to me. Maybe some "eval" construct would help?
I'm not good enough to try it.

Here is hwo i made it aget your suggestion:

unless( $name ) { print font( {class=>'tip'}, 'ÎÝ÷áóåò íá ìáò
ðåßò ðïéüò åßóáé!<p>' ); $i=1; }
unless( $pray ) { print font( {class=>'tip'}, 'Äåí ó÷ïëßáóåò ôçí
åõ÷Þ!<p>' ); $i=1; }
unless( $remark ) { print font( {class=>'tip'}, 'Äåí èá ìáò ðåßò
ãéá ôçí åìðåéñßá óïõ?<p>' ); $i=1; }
unless( $email ) { print font( {class=>'tip'}, 'ÓõìðëÞñùóå ôï
email óïõ!<p>' ); $i=1; }
exit 0 if ($i!=0);

but still not good enouph.

I really dont liek the use of $i.
I think there must me a smarter way of writing this.
 
S

Scott Bryce

Nikos said:
Here is hwo i made it aget your suggestion:

but still not good enouph.

Why not? Does it do what you want it to do? If not, what is it doing
that is different than what you expect?
I really dont liek the use of $i.

I think your use if $i is fine. You need some way of knowing whether an
error was encountered. $i serves that purpose. You may want to use a
more descriptive name such as $error_found.
I think there must me a smarter way of writing this.

I think it is fine. I would probably do something very similar if I
wrote it.
 
N

Nikos

Scott said:
I think your use if $i is fine. You need some way of knowing whether an
error was encountered. $i serves that purpose. You may want to use a
more descriptive name such as $error_found.



I think it is fine. I would probably do something very similar if I
wrote it.

ok if you say so! :)
 
S

Steven Kuo

On Fri, 29 Apr 2005, Henry Law wrote:

(snipped)
What he's trying to do is to make n tests for the non-true-evaluation
of n separate variables (he's presumably using that to infer their
zero-lengthness and therefore their non-existence), issue a customised
message for each one that is false, and then exit with a zero code if
there was at least one error. It's a fair question and I for one
can't find a neater way of doing it.

Here's a traditional ASCII codepage version, which makes it easier to
see:

# No strict, warnings - this is a fragment
# Untested for the same reason
# Probably crashes if $a or $b etc is undef

my $fail=0;
unless ($a) {
print "Error message for missing \$a\n";
$fail=1;
}
unless ($b) {
print "Different error message for missing \$b\n";
$fail=1;
}
# Etc for n different variables; his n=4
#

exit 0 if $fail;

print "We succeeded - they were all there\n";

Looks pretty clunky to me. Maybe some "eval" construct would help?
I'm not good enough to try it.



To refactor using 'eval', one could try:


use strict;
use warnings;
use CGI;

my ( $name, $pray, $remark, $email );

my %prompt_if_missing = (
name => "Something about a name.",
pray => "Something about prayer?",
remark => "A remark of some sort.",
email => "More spam please!",
);

$remark = 'foo';
$pray = 'for rain';

my $amiss;
for my $k (keys %prompt_if_missing)
{
my $field = eval "\$$k"; # please read perldoc -f eval
die $@ if $@;
unless (defined($field) and length $field)
{
print CGI::font(
{ class => 'tip' },
$prompt_if_missing{$k}
), "\n";
$amiss = 1;
}
}

exit if $amiss;


Given the experience level of the OP, however, it's probably best
that he avoid using 'eval' at present.
 
T

Tad McClellan

trying to do is to make n tests for the non-true-evaluation
of n separate variables (he's presumably using that to infer their
zero-lengthness and therefore their non-existence), issue a customised
message for each one that is false, and then exit with a zero code if
there was at least one error. It's a fair question and I for one
can't find a neater way of doing it.

Here's a traditional ASCII codepage version, which makes it easier to
see:

# No strict, warnings - this is a fragment
# Untested for the same reason
# Probably crashes if $a or $b etc is undef

my $fail=0;
unless ($a) {
print "Error message for missing \$a\n";
$fail=1;
}
unless ($b) {
print "Different error message for missing \$b\n";
$fail=1;
}
# Etc for n different variables; his n=4
#

exit 0 if $fail;

print "We succeeded - they were all there\n";

Looks pretty clunky to me. Maybe some "eval" construct would help?


No need for the full-monty eval evilness, the lesser evilness of
symrefs could be made to work.

If $a and $b are package variables then you could use symrefs,
but it would be better to have used a more appropriate data
structure (a hash) rather than a bunch of individual scalars...


my %msg = (
a => "Error message for missing \$a\n",
b => "Different error message for missing \$b\n"
);

my $fail=0;
foreach my $var ( 'a', 'b' ) {
no strict 'refs';
next if $$var;
print $msg{$var};
$fail=1;
}
# Etc for n different variables; his n=4
#

exit 0 if $fail;

print "We succeeded - they were all there\n";
 
B

Brian McCauley

Scott said:
I think it is fine. I would probably do something very similar if I
wrote it.

Yes, I largely agree with Scott. But I'd change the line

exit 0 if ($i!=0);

To the more ideomatic

exit 0 if $error_found;

(Condister the "read it aloud" test).

I'd also make $error_found a counter using ++ rather than a flag using
=1. It's not necessary and it's actually slightly slower but I find it
more idomatic.

Also since you (Nikos) do the same thing four times to four variables
($name,$pray,$remark,$email) you may want to consider makeing those a
hash. I general if you find you are doing very similar things to a
series of descrete scalars you probably really wanted an agregate.

But rewriting the chain of unless() statements as a loop would probably
only pay off in the short term until you got above four. Of course in
the longer term getting into the habit of always abstracting out anthing
you do _three_ times is good and I see that you (Nikos) sense that.

Does it matter the order in which the tips appear? If not you can put
them in a hash and loop over it. (But only if the fields are in a hash
too).

On a non-Perl issue I suspect the <span> HTML tag would be more
appropriate than the deprocated <font> one. Actually you appear to have
misunderstood what <p> means in HTML. You are confusing it with <br>. It
is illegal (an hense undefined what would happen) to put a <p> inside a
<font> or <span>.

Probably all in all you want a <div>.

my %tips = (
name => 'Name tip',
pray => 'Pray tip',
remark => 'Remark tip',
email => 'Email tips',
);

my $error_found;

for ( keys %tips ) {
unless ( $field{$_} ) {
print div( {class=>'tip'}, $tips{$_});
$error_found++;
}
}

exit 0 if $error_found;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top