Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: assigning arrays as values to keys of hash

by BillKSmith (Prior)
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 #!/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 $VAR1 = { 'snake' => [ 'fangs', 'scales', 'tail' ], 'bird' => [ 'beak', 'claw', 'wings', 'feathers' ] }; ?

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2020-08-11 15:41 GMT
Find Nodes?
    Voting Booth?
    Which rocket would you take to Mars?

    Results (62 votes). Check out past polls.