in reply to sorting and other problems in hash

That was a lot of code to try to get what you wanted. I used a (to me) simpler approach:

  1. Read in file1
    1. Looping through the file
      1. If a blank line or only contains whitespace, skip
      2. If the line begins with a '>', split on whitespace to get the sequence name
      3. Otherwise, split the line on whitespace into an array in a hash, keyed by the sequence name
  2. Read in file2
    1. Looping through the file
      1. If a blank line or only contains whitespace, skip
        1. If the line begins with a '>'
        2. Split the line on understore, equal, or whitespace to get the sequence name, "side", and length
        3. If the sequence is not defined, display a warning
        4. Generate the range based on the "side"
        5. Print the line
        6. Print the digit sequence range
      2. Otherwise, display the line

Or, in code:

#!/usr/bin/perl use strict; use warnings; if ( scalar @ARGV < 2 ) { die("Usage:\n\t$0 file1 file2\n\n"); } my %data; my $fn = $ARGV[0]; { my $sequence; open DF, $fn or die $!; while ( my $line = <DF> ) { chomp $line; next if $line =~ m/^\s*$/; if ( substr( $line, 0, 1 ) eq '>' ) { ( $sequence, undef ) = split /\s+/, $line; } else { @{ $data{$sequence} } = split /\s+/, $line; } } close DF; } $fn = $ARGV[1]; { my $sequence; my $side; my $len; open DF, $fn or die $!; while ( my $line = <DF> ) { chomp $line; next if $line =~ m/^\s*$/; if ( substr( $line, 0, 1 ) eq '>' ) { ( $sequence, $side, undef, $len ) = split /(?:_|=|\s)+/, $line; if ( defined @{ $data{$sequence} } ) { my @range = ( 0, $len - 1 ); if ( $side ne "left" ) { @range = ( -1 * $len, -1 ); } print $line, "\n"; print join( ' ', @{ $data{$sequence} } [ $range[0] .. $range[1] ] ), "\n"; } else { warn "No data read from " . $ARGV[0] . " for sequence $sequence\n"; } } else { print join( " ", split //, $line ), "\n"; } } close DF; }

Hope that helps.