This is a commented version of your script along with hopefully improved code. I think that will take you further.
#!/usr/bin/perl use warnings; use strict; use Data::Dumper; my $fh1 = "File1.txt"; my $fh2 = "File2.txt"; #> so far, so good. though in a flexible program at least better writt +en as #> that way you can call it: perl scriptname file1 file2 # my ($position_file, $sequence_file) = @ARGV; open(FH, "$fh1") || die ("Can't open: $!"); my @AoH; #> that's "ok", too. three quirks: - unnecessary quoting. if you mean +the plain value of a variable #> there's no need to quote it ("") - The use of a Filehandle. Best pr +actice are lexical variables for filehandles. #> - variable names: for sake of you own sanity: use DESCRIPTIVE varia +ble names. ALWAYS. #> so: # open(my $file, '<', $position_file) || die ("Can't open $position_fi +le: $!"); # my %positions; while ( <FH> ) { push @AoH, { split /[\s]+/ }; } # this will not do what you think. it creates a hashref from the a +rray split of your positions file # this will trigger the "Odd number of elements in anonymous hash +at ashnator.pl line 12, <FH> line 1." warnings # you want: # Create a hash of arrays of positions # while ( <$file> ) { # my ($key, $pos) = split /\s+/; # if (!$positions{$key}) { # #create array at $key for the first position per key # $positions{$key} = [$pos]; # } else { # # add position for all subsequent lines # my $ref = $positions{$key}; # push @$ref,$pos; # } # } my %sequences; { #> uneccessary block open(FD, "$fh2") || die("Cannot open: $!"); my $key; while (<FD>) { if ( s/^>// ) { $key = ( split /\|/ )[1]; } else { chomp; $sequences{$key} .= $_; } } } #> print Dumper(\%sequences); #> same critics than above, but this time the loop works and i assume +you understand how. for my $href ( @AoH ) { for my $role ( sort keys %$href ) { my $key; # why $key when $role is the loop variable? #> RULE OF THUMB: Think before you copy and paste! #> because the data structure is now a bit different we make this: #> while ( my ($key, $position_list) = each %positions ) #> { #> for my $position ( @$position_list ) #> { #> print "key: $key, position: $position\n"; #> # ... your inner loop code } }
Update: added @ARGV explanation


holli, /regexed monk/

In reply to Re^5: howto accomodate multiple values by holli
in thread howto accomodate multiple values by aspirado

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.