Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
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 rifling through the Monastery: (13)
As of 2015-07-01 16:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (12 votes), past polls