multiples ifs

Discussion in 'Perl Misc' started by Nikos, Apr 29, 2005.

  1. Nikos

    Nikos Guest

    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);
     
    Nikos, Apr 29, 2005
    #1
    1. Advertising

  2. Nikos <> wrote in news:d4tbuq$4ui$:

    > 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

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Apr 29, 2005
    #2
    1. Advertising

  3. Nikos

    Nikos Guest

    Bernard El-Hagin wrote:
    > Nikos <> wrote:
    >
    >
    >>is there any better way to write this in Perl?

    >
    >
    >
    > 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.
     
    Nikos, Apr 29, 2005
    #3
  4. Nikos <> wrote in news:d4td50$5u9$:

    > Bernard El-Hagin wrote:
    >> Nikos <> wrote:
    >>
    >>>is there any better way to write this in Perl?

    >>
    >> 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.


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

    Sinan

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Apr 29, 2005
    #4
  5. Nikos

    Nikos Guest

    A. Sinan Unur wrote:

    > 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?
     
    Nikos, Apr 29, 2005
    #5
  6. Nikos

    Henry Law Guest

    On Fri, 29 Apr 2005 16:14:03 +0300, Nikos <> wrote:

    >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.
     
    Henry Law, Apr 29, 2005
    #6
  7. Nikos

    Nikos Guest

    Henry Law wrote:
    > On Fri, 29 Apr 2005 16:14:03 +0300, Nikos <> wrote:
    >
    >
    >>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.


    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.
     
    Nikos, Apr 29, 2005
    #7
  8. Nikos

    Scott Bryce Guest

    Nikos wrote:

    > Here is hwo i made it aget your suggestion:


    <code snipped>

    > 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.
     
    Scott Bryce, Apr 29, 2005
    #8
  9. Nikos

    Nikos Guest

    Scott Bryce wrote:

    >> 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.


    ok if you say so! :)
     
    Nikos, Apr 29, 2005
    #9
  10. Nikos

    Steven Kuo Guest

    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.
     
    Steven Kuo, Apr 29, 2005
    #10
  11. Henry Law <> wrote:


    > 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";



    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Apr 29, 2005
    #11
  12. Scott Bryce wrote:

    > Nikos wrote:
    >
    >> 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.


    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;
     
    Brian McCauley, Apr 30, 2005
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Richard Loupatty
    Replies:
    2
    Views:
    372
    Richard Loupatty
    Jul 18, 2003
  2. FD
    Replies:
    0
    Views:
    823
  3. Carl Friedrich Bolz

    Re: Multiples of a number

    Carl Friedrich Bolz, Nov 6, 2005, in forum: Python
    Replies:
    2
    Views:
    314
    Robert Kern
    Nov 6, 2005
  4. Efrat Regev

    One-step multiples list generation?

    Efrat Regev, Jan 3, 2006, in forum: Python
    Replies:
    7
    Views:
    331
    Steven D'Aprano
    Jan 4, 2006
  5. blaine
    Replies:
    6
    Views:
    441
    blaine
    Apr 28, 2008
Loading...

Share This Page