Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Confused by a Conditional

by damian1301 (Curate)
on Jun 10, 2001 at 10:27 UTC ( #87277=note: print w/ replies, xml ) Need Help??


in reply to Confused by a Conditional

delete

Tiptoeing up to a Perl hacker.
Dave AKA damian


Comment on Re: Confused by a Conditional
Re: Re: Confused by a Conditional
by CheeseLord (Deacon) on Jun 10, 2001 at 12:23 UTC

    I admit, that's pretty cool. But I personally like this just a bit better (only because it takes up less space on the line):

    #!/usr/bin/perl -w use strict; my $key = 'JUMP'; if ($key =~ /^(ID|TITLE|GENE|CYTOBAND|LOCUSLINK|CHROMOSOME|SCOUNT)$/) +{ print "I don't know why this is printing.\n"; }

    I think there's someway that might be broken, though, if I remember my reading on regexes... but if you chomp everything right, I think it will accomplish the same thing (with the added benefit(?) of placing exactly what it matched in $1).

    CheeseLord

Re: Confused by a Conditional
by Abigail (Deacon) on Jun 10, 2001 at 18:09 UTC
    You are very, very wrong.
    'ID' or 'TITLE' or 'GENE' or 'CYTOBAND' or 'LOCUSLINK' or 'CHROMOSOME' or 'SCOUNT'
    evaluates to 'ID'.
    use strict; my $key = 'TITLE'; unless (($key) eq ('ID' or 'TITLE' or 'GENE' or 'CYTOBAND' or 'LOCUSLINK' or 'CHROMOSOME' or 'SCOUNT')) { print "Damian is wrong\n"; }
    This prints "Damian is wrong".

    -- Abigail

      delete

      Tiptoeing up to a Perl hacker.
      Dave AKA damian

        Oh, goodie, you used an if. Care to quote the manual where it says that in the context of an if, eq propagates some magic to or to do what you suggest it is doing?

        Anyway, the following code uses if instead of unless, and it does not print "Damian is right"....

        #!/usr/bin/perl -w use strict; my $key = 'LOCUSLINK'; if (($key) eq ('ID' or 'TITLE' or 'GENE' or 'CYTOBAND' or 'LOCUSLINK' or 'CHROMOSOME' or 'SCOUNT')) { print "Damian is right\n"; }

        Your code only happens to do the right think if $key equals ID, or it's different from any of the clauses. It'll fail to work when the key equals TITLE, GENE, CYTOBAND, LOCUSLINK, CHROMOSOME, or SCOUNT.

        -- Abigail

Re: Re: Confused by a Conditional
by ZZamboni (Curate) on Jun 10, 2001 at 20:12 UTC
    The only way the conditional in your snippet evaluates to true is if $key=='ID'. Try setting $key to 'GENE', for example (which is one of the valid values), and you'll see.

    --ZZamboni

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://87277]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (18)
As of 2014-08-27 14:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (239 votes), past polls