Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: set context for method's return values

by Marshall (Canon)
on Jan 27, 2022 at 04:09 UTC ( [id://11140889]=note: print w/replies, xml ) Need Help??


in reply to set context for method's return values

Your code:
print(split(",", "A,B,C,D")[2]);
Yields:
syntax error at XXX.pl line YYY, near ")["
I think what you want is:
print "",(split(",", "A,B,C,D"))[2];
print can take a file handle as an optional arg and this can be confusing.
putting a dummy null string in front disambiguates this. The split creates a list, put the list into paren and use the bracketed 2 to get C.

Basically don't put a Perl variable or a function call immediately after the keyword "print". Adding more parens is a possible solution, but I prefer the above. Mileage varies.

Log In?
Username:
Password:

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

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

    No recent polls found