Why do it in two passes when it can be done in one pass? Efficiency is over-rated. (tybalt89 ducks :)
#!/usr/bin/perl # https://perlmonks.org/?node_id=1222551 use strict; use warnings; use Data::Dumper; my %hash; while( <DATA> ) { /(\S+)\s+(\S+)/ and $hash{$1} = [ keys %{{map {$_, 1} @{$hash{$1}}, +$2}} ]; } print Dumper \%hash; __DATA__ snake fangs snake tail snake fangs bird feathers bird beak snake scales bird beak bird claw bird wings
In reply to Re: assigning arrays as values to keys of hash
by tybalt89
in thread assigning arrays as values to keys of hash
by pearllearner315
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |