Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comarison synthax question

by perlknight (Pilgrim)
on Apr 11, 2007 at 20:28 UTC ( [id://609467]=perlquestion: print w/replies, xml ) Need Help??

perlknight has asked for the wisdom of the Perl Monks concerning the following question:

Fellow monks, I have this line of code:
if (($STATUS1 & $STATUS2) =~ m/^Running$/i) { print "match\n"; }
Is the above doing what I think it is doing, making sure that both status1 and status2 is matches the expression "Running"? Or should I be explicit about it, like so:
if (($STATUS1 =~ m/^Running$/i) & ($STATUS2 =~ m/^Running$/i)) { print "match\n"; }

Replies are listed 'Best First'.
Re: comarison synthax question
by bobf (Monsignor) on Apr 11, 2007 at 20:41 UTC

    It is most definitely not doing what you think it is.

    & is the bitwise string operator (see perlop). If you want a logical comparison, you need to use and or && (just be aware of the difference in precedence between the two; again, see perlop).

    To test both variables against the regex, you need to test each of them individually. As an aside, if you didn't need the case insensitivity you could use index.

    I think you'll get the results you desire if you fix your second example to use && instead of & .

Re: comarison synthax question
by Fletch (Bishop) on Apr 11, 2007 at 20:42 UTC

    You're getting closer with the second, but && is the boolean AND operator (& is the bitwise AND; see perlop for more details).

    And using grep might be a bit more idiomatic, especially since the numbered variable names is a common red flag that you should really be using an array instead.

    my @statuses = _frobulate_status_processes( ); my $running_frobulators = grep { m/\A Running \z/xi } @statuses; if( @statuses == $running_frobulators ) { print "All processes frobulating\n"; } else { print "Only $running_frobulators of ", scalar @statuses, " running\n +"; }
Re: comarison synthax question
by thezip (Vicar) on Apr 11, 2007 at 20:47 UTC

    Actually, neither will currently work, but the second option is closer to what you want to do.

    You'll need to use either 'and' or '&&' to conjoin the logical expressions, which must be explicitly stated as you did in the latter code.

    if (($STATUS1 =~ m/^Running$/i) && ($STATUS2 =~ m/^Running$/i)) { + print "match\n"; ^^ }
    You could also use:
    if (($STATUS1 eq "Running") && ($STATUS2 eq "Running")) { print "match\n"; }

    ... if you could expect the upper/lower case not to change. You've already tightened it down pretty hard with /^...$/, so a test for equality might also be appropriate.


    Where do you want *them* to go today?
Re: comarison synthax question
by Krambambuli (Curate) on Apr 11, 2007 at 20:41 UTC
    my $STATUS1 = "Running"; my $STATUS2 = "Sunning"; print $STATUS1 & $STATUS2, "\n";
    prints 'Running', as R is 0x52 and S = 0x53, but R & S = R

    So _no_, don't use '&' unless you know you're working on bits.

    I guess you might have it also like
    if (lc($STATUS1) eq lc($STATUS2) and lc($STATUS1) eq 'running') {... }
    Why not being explicit whenever possible ?
Re: comarison synthax question
by ptum (Priest) on Apr 11, 2007 at 20:43 UTC

    Well, for starters you'll probably want to use && instead of &, which is a bitwise 'and'. See perlop.

    Update: As to your main question, I see that others beat me to the punch. :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://609467]
Approved by bobf
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-03-29 09:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found