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

Re: assigning arrays as values to keys of hash

by Marshall (Abbot)
on Sep 18, 2018 at 19:00 UTC ( #1222603=note: print w/replies, xml ) Need Help??


in reply to assigning arrays as values to keys of hash

I guess while we are beating this thing to death...
I find this pretty easy to read... List::Util is great module.

#!/usr/bin/env perl use strict; use warnings; use Data::Dumper; use List::Util qw(uniq); my %hash; while (<DATA>) { my ($animal, $part) = split /\s+/; push @{$hash{$animal}},$part; } @{$hash{$_}} = uniq @{$hash{$_}} for keys %hash; print Dumper \%hash; __DATA__ bird beak bird beak bird claw bird wings bird feathers snake fangs snake scales snake fangs snake tail

Replies are listed 'Best First'.
Re^2: assigning arrays as values to keys of hash
by AnomalousMonk (Chancellor) on Sep 18, 2018 at 23:17 UTC
    @{$hash{$_}} = uniq @{$hash{$_}} for keys %hash;

    I think
        @$_ = uniq @$_ for values %hash;
    is more concise and even easier to read :)


    Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2019-07-18 09:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?