So you have three arrays, each of which is a unique list of numbers, and you want to construct @locations as a list of where they came from, in order. You also construct @new_gp, which looks like it should be the pairs list flattened out.
When you need to look up a mapping, like a number mapped to its source, use a hash.
use strict;
use warnings;
# Just generating data here.
my @rep1 = (1..5);
my @rep2 = (6..10);
my @rep3 = (11..15);
my @pairs = map {sprintf "%d %d", int(rand(15)+1), int(rand(15)+1)} 1.
+.10;
# Here's the magic, fairly information-dense.
# Make a list of name-array associations, pass it to map
# Then go through the members of the array and associate each member
# with the name.
my %sources = map {
my ($name, $ref) = @$_;
map {($_ => $name)} @$ref;
} ([rep1 => \@rep1], [rep2 => \@rep2], [rep3 => \@rep3]);
# Now, when you have a number $n1, its source is $sources{$n1}
my (@locations, @new_gp);
for (@pairs) {
print "$_: ";
my ($n1, $n2) = split;
push @new_gp, $n1, $n2;
push @locations, @sources{$n1, $n2};
print "$n1 came from $sources{$n1}; $n2 came from $sources{$n2}\n"
+;
}
Caution: Contents may have been coded under pressure.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.