Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: assigning arrays as values to keys of hash

by BillKSmith (Parson)
on Sep 18, 2018 at 14:33 UTC ( #1222589=note: print w/replies, xml ) Need Help??


in reply to assigning arrays as values to keys of hash

It is possible to do exactly what you asked for by explicitly testing for duplicates (use the function 'none' from List::Util) before storing. Because it uses arrays, it does preserve the order of the features. This solution is probably the slowest running of all the suggestion you received.
?type pearllearner315.pl #!/usr/bin/perl use strict; use warnings; use List::Util qw(none); use Data::Dumper; my $file = \<<'EOF'; bird beak bird beak bird claw bird wings bird feathers snake fangs snake scales snake fangs snake tail EOF #my $file = 'file.txt'; open( FILE, '<', $file ) or die $!; my %hash; while ( <FILE> ) { chomp; my $lines = $_; my ($key, $value) = split(/\s+/, $lines, 2); push @{ $hash{$key} }, $value if none {$value eq $_} @{$hash{$key} +}; } print Dumper(\%hash); ?perl pearllearner315.pl $VAR1 = { 'snake' => [ 'fangs', 'scales', 'tail' ], 'bird' => [ 'beak', 'claw', 'wings', 'feathers' ] }; ?
Bill

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2019-07-20 17:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?