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.


In reply to Re: sorting and other problems in hash by atcroft
in thread sorting and other problems in hash by sugar

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.