in reply to extracting information from arrays
Starting with sample data like what was suggested in an earlier reply:
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.# 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"; }
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.
|
|---|