Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Re: Re: Re: Elegantly Selecting Every 3rd Element in an Array.

by gbarr (Monk)
on Sep 29, 2001 at 03:03 UTC ( [id://115536]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Re: Elegantly Selecting Every 3rd Element in an Array.
in thread Elegantly Selecting Every 3rd Element in an Array.

It prints nothing because you have a precedence problem

not ($_+1) % 3

Will always evaluate to false as it is the same as

(not ($_+1)) % 3

Adding some parens in the right places

not (($_+1) % 3)

Should give the correct result

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Elegantly Selecting Every 3rd Element in an Array.
by runrig (Abbot) on Sep 29, 2001 at 03:15 UTC
    Why do the parens change the precedence of the 'not'?? Seems like 'not' should still have the lowest precedence. Compare these two (which should return every third element, but start with the first), and tell me why they should be different:
    not $_ % 3 not ($_) % 3
    Update:tye explains below. My opinion (on current precedence rules, not tye's opinion) above.

      It is the old "if it looks like a function call" rule. Most commonly noted for: print ($this),$that getting parsed as print($this), $that but it also applies to other things, including the not "function". Update, the application of this particular rule to not appears to have been introduced in Perl 5.6.0, so it might even be a bug.

      BTW, the proof that this is the problem is that adding a unary "plus": not +($_+1) % 3 fixes it.

              - tye (but my friends call me "Tye")

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-19 15:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found