Stuff file2 contents into a Perl data structure, such as a hash of hashes, then print desired file1 contents using array slices:
use strict; use warnings; my $fh; open $fh, '<', 'file2.txt' or die "cant open file2.txt: $!"; my %info; while (<$fh>) { if (/^>(.*)_left\s\w+=(\d+)/) { $info{$1}{left} = $2; } if (/^>(.*)_right\s\w+=(\d+)/) { $info{$1}{right} = $2; } } close $fh; # comparing with file 1 open $fh, '<', 'file1.txt' or die "cant open file1.txt: $!"; my $id; while (<$fh>) { if (/^>(\w+)/) { $id = $1; } else { my @nums = split; my $len = $info{$id}{left}; print ">${id}_left length=$len\n"; print "@nums[0..($len-1)]\n"; $len = $info{$id}{right}; print ">${id}_right length=$len\n"; print "@nums[($#nums-$len)..$#nums]\n"; } } __END__ >AAAT3R_left length=6 40 40 40 40 40 40 >AAAT3R_right length=62 40 40 40 40 40 40 38 38 37 39 36 36 40 36 35 35 35 38 40 35 35 33 35 3 +5 35 40 40 40 40 37 37 38 38 38 40 40 40 40 40 40 40 40 40 40 40 40 4 +0 40 40 40 37 36 36 31 22 22 22 20 20 20 20 20 14 >AAA2OJ_left length=14 18 18 18 21 35 35 35 32 32 32 33 35 38 39 >AAA2OJ_right length=14 37 37 37 37 37 37 33 32 32 30 20 17 17 17 0

In reply to Re: sorting and other problems in hash by toolic
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.