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

Re: Anonymous Arrays or Anonymous Array References

by Anonymous Monk
on Mar 03, 2011 at 15:53 UTC ( #891249=note: print w/replies, xml ) Need Help??


in reply to Anonymous Arrays or Anonymous Array References

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 }};

Replies are listed 'Best First'.
Re^2: Anonymous Arrays or Anonymous Array References
by moritz (Cardinal) on Mar 03, 2011 at 16:26 UTC
Re^2: Anonymous Arrays or Anonymous Array References
by ikegami (Pope) on Mar 03, 2011 at 19:07 UTC

    @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: note [id://891249]
help
Chatterbox?
and all is quiet...

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