Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Anonymous Arrays or Anonymous Array References

by Anonymous Monk
on Mar 03, 2011 at 15:17 UTC ( #891237=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

print (1,2,3)[1];
Why does the above not work, but the below does?
print [1,2,3]->[1];
And don't just say because the second is unambiguous!

Comment on Anonymous Arrays or Anonymous Array References
Select or Download Code
Re: Anonymous Arrays or Anonymous Array References
by kennethk (Monsignor) on Mar 03, 2011 at 15:24 UTC
    When you write print (1,2,3)[1], the interpreter associates the parentheses with the function call, not with a list assignment. In that interpretation, the array index is a meaningless syntax error. To get the functionality you expect, you require print( (1,2,3)[1] ); Your second case is equivalent to print( [1,2,3]->[1] );. This is all a consequence of letting parentheses be optional for built-ins, and is discussed a bit in perlsyn.
Re: Anonymous Arrays or Anonymous Array References
by Fletch (Chancellor) on Mar 03, 2011 at 15:35 UTC

    The first would work if you provided an extra set of parens or unary plus to make it parse the way you intended it to in light of the optional paren behavior.

    $ perl -MO=Deparse,-q,-p -e 'print +(1,2,3)[1];' print(((1, 2, 3))[1]); -e syntax OK $ perl -MO=Deparse,-q,-p -e 'print( (1,2,3)[1] );' print(((1, 2, 3))[1]); -e syntax OK

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: Anonymous Arrays or Anonymous Array References
by Anonymous Monk on Mar 03, 2011 at 15:53 UTC
    My eyes are opened! Then is there a way to coerce an array with parenthesis into a hash?
    @U = keys ('A'=>1,'B'=>2,'Z'=>26);
    Mostly I have been using the following approach:
    @U = keys %{{ map { $_=>1} @NU }};

      @U = keys ('A'=>1,'B'=>2,'Z'=>26);

      That's the same as getting every second item (starting with the first) and removing duplicates. There's no good shorthand for that, even if you omit the requirement to remove duplicates.

      @U = keys %{{ map { $_=>1} @NU }};

      If the contents of @NU has no duplicates, then that's the same as

      my @U = @NU;

      If your question is how to remove duplicates, then there's an answer in perlfaq4. Some solutions

      my @U = uniq @NU; # From List::Util

      I use the idiomatic

      my %seen; my @U = grep !$seen{$_}++, @NU;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2014-09-21 08:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (168 votes), past polls