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

Re: 'and next' question

by hanenkamp (Pilgrim)
on Oct 26, 2003 at 20:56 UTC ( [id://302265]=note: print w/replies, xml ) Need Help??


in reply to 'and next' question

Try

++$hash{$_} and next if $_ > 5;

Update: Thanks Anonymous Monk. You're right, I was wrong. Please ignore the rest of this...it doesn't hold water.

Be careful though about using and this way. Since the precedence of and is extremely low, this is the same as saying:

# Increment $hash{$_} then if $_ > 5 loop next ++$hash{$_} and (next if $_ > 5);

When you might rather mean:

# If $_ > 5 then increment $hash{$_} and loop next (++$hash{$_} and next) if $_ > 5;

It depends on when you want %hash to be changed. Often, I will avoid statements like this at the cost of brevity to avoid misunderstanding between myself and the compiler on this sort of nuance--or at least use paren's to make the situation explicit.

Replies are listed 'Best First'.
Re: Re: 'and next' question
by Anonymous Monk on Oct 26, 2003 at 21:51 UTC
    ++$hash{$_} and next if $_ > 5;
    Be careful though about using and this way. Since the precedence of and is extremely low, this is the same as saying:
    # Increment $hash{$_} then if $_ > 5 loop next ++$hash{$_} and (next if $_ > 5);
    No, it is not the same at all. The if is a statement modifier and will always be tested first.
    ++$hash{$_} and next if $_ > 5;
    is, as one would expect, the same as:
    $_ > 5 and (++$hash{$_} and next);

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-03-29 02:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found