in reply to assigning arrays as values to keys of hash
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
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: assigning arrays as values to keys of hash
by Marshall (Canon) on Sep 18, 2018 at 22:29 UTC | |
by tybalt89 (Monsignor) on Sep 18, 2018 at 23:06 UTC | |
by Marshall (Canon) on Sep 18, 2018 at 23:50 UTC | |
by Anonymous Monk on Sep 18, 2018 at 22:45 UTC | |
by tybalt89 (Monsignor) on Sep 18, 2018 at 23:36 UTC | |
by Marshall (Canon) on Sep 18, 2018 at 23:33 UTC |
In Section
Seekers of Perl Wisdom