my $a=shift @COUNT; my @families=split(/\s+/,$a); my %participants; foreach (@COUNT) { my @a=split(/\s+/,$_); my $og=$a[0]; my $search_for="1"; ## search for participants in that OG my ( $index )= grep { $a[$_] eq $search_for } 0..$#a; $participants{$og}=$index; } my %og_to_gid; foreach my $k (keys %participants) { $og_to_gid{$k}=$families[$participants{$k}]; }