Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Hi Monks,

Could I pick your brains regarding some array wrangling?

I have an input file that looks something like:

Family lacM taba mori glyB gly4 OG_1 1 0 1 0 0 OG_2 0 1 0 1 0

and I would like to know, for each family, which two members are '1', possibly storing them as a hash in which the key = family (eg OG_1) and the value = 'lacM mori' or something similar...

I am able to manipulate this such that I can get the name of the first incidence of '1', by using (the input file has been read into the array @COUNT):

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

which gives, within the %og_to_gid hash, a result whereby the key = OG_1 and the value = lacM, in this case (sorry about the weird variable names; I'm a genome biologist (and genome biologists are weird anyway)), which was fine for a previous script but now I really need to know the names of BOTH the members for each family.

Any ideas would be most welcome! Is there something within for example List::Utils that could do it? I had a look but I couldn't see anything...

Thanks once again,


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

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (9)
    As of 2018-06-20 17:56 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (117 votes). Check out past polls.