Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^2: arrays: shifting in while loop

by ivanthemad (Initiate)
on Mar 06, 2012 at 14:47 UTC ( [id://958084]=note: print w/replies, xml ) Need Help??


in reply to Re: arrays: shifting in while loop
in thread arrays: shifting in while loop

Thank you, you are correct! Re-examining my code in the interim between posting and your timely response, I noticed that I was in fact receiving a warning:
print (...) interpreted as function at - line 7.
Thank you for the clear explanation which has rectified my misunderstanding! I will also examine further your use of -MO=Deparse,p -e, which looks to be very useful indeed.

Replies are listed 'Best First'.
Re^3: arrays: shifting in while loop
by kcott (Archbishop) on Mar 06, 2012 at 15:09 UTC

    When you get a message like "print (...) interpreted as function ...", you can typically disambiguate your statement for Perl by simply adding a + in front of the opening parenthesis.

    Your original code runs like this:

    ken@ganymede: ~/local/bin $ perl -Mstrict -Mwarnings -E 'my $s = "foo bar baz qux 3 3 1 3"; my @ +a = split /\s/, $s; splice(@a, 0, 4); while (@a) { print (shift @a), +(shift @a), "\n"; }' print (...) interpreted as function at -e line 1. Useless use of a constant ( ) in void context at -e line 1. 31ken@ganymede: ~/local/bin $

    Adding a single + (changing ... print (... to ... print +(...), you get the output you were after:

    ken@ganymede: ~/local/bin $ perl -Mstrict -Mwarnings -E 'my $s = "foo bar baz qux 3 3 1 3"; my @ +a = split /\s/, $s; splice(@a, 0, 4); while (@a) { print +(shift @a), + (shift @a), "\n"; }' 33 13 ken@ganymede: ~/local/bin $

    This is discussed in more detail in perlop - Symbolic Unary Operators.

    -- Ken

Re^3: arrays: shifting in while loop
by Anonymous Monk on Mar 06, 2012 at 14:50 UTC
Re^3: arrays: shifting in while loop
by JavaFan (Canon) on Mar 06, 2012 at 15:07 UTC
    Do note you only get said warning because you use exactly one space between print and the parenthesis. Drop the space, use 2 spaces, or a tab, and the warning disappears. Replace print by warn, and the warning disappears as well.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-26 00:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found