The way I'd probably do this is to organize the three source arrays into a hash structure (HoH: hash of hashes), where the top-level hash keys are the names of the three sources, and the lower level hash keys are the values in each source array. That way, looking up the source for a given value is just a matter of checking for the existence of a given hash key.

Starting with sample data like what was suggested in an earlier reply:

# some sample data: my @rep1_pids = (1..10); my @rep2_pids = (11..20); my @rep3_pids = (21..30); my @pairs = ( "11\t5", "22\t14", "4\t8", "1\t29" ); # organize sources into HoH (%arrayhash): # create %hashbuilder first, to assign source names to arrays my %hashbuilder = ( rep1 => \@rep1_pids, rep2 => \@rep2_pids, rep3 => \@rep3_pids, ); # for each source, array names will be the primary hash keys, # and the array values will become keys of the secondary hashes: my %arrayhash; for my $src ( keys %hashbuilder ) { $arrayhash{$src}{$_} = 1 for ( @{$hashbuilder{$src}} ); } # now identify sources for the members (items) in each pair: for my $pairstr ( @pairs ) { my $srcs = ''; for my $item ( split( /\t/, $pairstr )) { for my $src ( sort keys %arrayhash ) { if ( $arrayhash{$src}{$item} ) { $srcs .= "$item is from $src; "; } } } print $srcs, "\n"; }
It wasn't clear from the OP if this produces the format you want, but it should be easy to change it to your preference.

Actually, there was a lot in the OP code that looked pretty strange and unclear; like, if the array of pairs is a set of strings like "1304\t1509", what do you think you're accomplishing with the first couple lines of the "foreach $pair" loop? And what's the purpose of the "@location" array? (You never seem to use it after pushing stuff into it.) And so on.

Let me suggest that when you write code, you start by documenting the plan for what the code is supposed to do: what its inputs and outputs should be, and (in terms understandable to an intelligent person) what the steps are that transform the input into the output. Then write the code to satisfy that description. If you have trouble writing the code, consider changing the description and trying again; if it seems too complicated to describe effectively, break it up into chunks that are easier to describe separately (those chunks become subroutines or modules).

My point is, if you can't describe the steps your code is supposed to be going through, it's likely to end up being a mess.


In reply to Re: extracting information from arrays by graff
in thread extracting information from arrays by Anonymous Monk

Title:
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, details, 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, summary, 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.