Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Getting indices of the same value that occurs multiple times in an array...

by AnomalousMonk (Abbot)
on Aug 04, 2011 at 14:55 UTC ( #918549=note: print w/ replies, xml ) Need Help??


in reply to Getting indices of the same value that occurs multiple times in an array...

>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 ]


Comment on Re: Getting indices of the same value that occurs multiple times in an array...
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2014-12-25 16:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (160 votes), past polls