in reply to match with elements in array
Here is another version using the arrays as listed in OP and a reference that loops through each array. You could add logic to break out of the loops once the value is found.
Updated again... left out $found declaration and initialization (fat fingers).
#!c:\perl\bin\perl.exe use strict; use warnings; my @lane01_1 = (200900..202543); my @lane01_2 = (202544..204187); my @lane01_3 = (204188..205831); my @lane01_4 = (205832..207475); my @lane01_5 = (207476..210119); my @lane01_6 = (210120..211763); my @lane01_7 = (211764..213407); my @lane01_8 = (213408..215051); my $arrayref; my $buildvar; my $vector = 209456; my $found = 0; while(! $found) { $buildvar = "\\\@" . "lane01_" . $i; $arrayref = eval($buildvar); if(! defined @$arrayref) { print("vector: $vector not found\n"); last; } for(my $j = 0; $j < (scalar(@$arrayref)); $j++) { if($$arrayref[$j] == $vector) { print("lane # is $i\n"); $found = 1; } } $i++; } OUTPUT: lane # is 5
Update #1: Removed some values from testing
Update #2: Included output.
Update #3: Couldn't take it - replaced outer for loop with a while loop and a bool. This also required a test to make sure @$arrayref references a valid array. If it's not, then there is no match for $vector. Original for loop included below.
for (my $i = 1; $i <= 8; $i++) { $buildvar = "\\\@" . "lane01_" . $i; $arrayref = eval($buildvar); for(my $j = 0; $j < (scalar(@$arrayref)); $j++) { if($$arrayref[$j] == $vector) { print("lane # is $i\n"); } } }
|
|---|