Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Anonymous Arrays or Anonymous Array References

by Anonymous Monk
on Mar 03, 2011 at 15:53 UTC ( [id://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 (Patriarch) 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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://891249]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-04-19 23:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found