>perl -wMstrict -le "my @COUNT = ( 'Family lacM taba mori glyB gly4', 'OG_1 1 0 1 0 0', 'OG_2 0 1 0 1 0', ); ;; my @families = split /\s+/, shift @COUNT; ;; my %participants; for my $record (@COUNT) { my @fields = split /\s+/, $record; $participants{$fields[0]} = [ map { $families[$_] } grep { $fields[$_] } 1 .. $#fields ]; } ;; use Data::Dumper; print Dumper \%participants; " $VAR1 = { 'OG_2' => [ 'taba', 'glyB' ], 'OG_1' => [ 'lacM', 'mori' ] };
Update: The expression
[ map { $families[$_] } grep { $fields[$_] } 1 .. $#fields ]
is more concise and perhaps a bit faster as
[ map $families[$_], grep $fields[$_], 1 .. $#fields ]
In reply to Re: Getting indices of the same value that occurs multiple times in an array...
by AnomalousMonk
in thread Getting indices of the same value that occurs multiple times in an array...
by reubs85
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |