You need to keep track of previously viewed rows. Assuming the data is sorted by sequence id,

my $last_seq; while (<>) { my ($seq, $p1, $p2) = (split)[0, 2, 3]; ($p2, $p1) = ($p1, $p2) if $p2 < $p1; if (defined($last_seq)) { if ($seq eq $last_seq) { print(","); } else { print("\n$seq "); } } print("$p1..$p2"); $last_seq = $seq; } print("\n") if defined($last_seq);

Update: More like your code and less like your explanation:

sub extractseq { my ($seq, $ranges) = @_; system(extractseq => "-sequence=$seq.seq", "-auto", "-stdout", "-separate", "-reg=" . join(',', map { "$_->[0]..$_->[1]" } @$ranges), ) and die("system: $?/$!\n"); } my $last_seq; my @ranges; while (<>) { my ($seq, $p1, $p2) = (split)[0, 2, 3]; ($p2, $p1) = ($p1, $p2) if $p2 < $p1; if (defined($last_seq) && $seq ne $last_seq) { extractseq($last_seq, \@ranges); @ranges = (); } $last_seq = $seq; push @ranges, [ $p1, $p2 ]; } extractseq($last_seq, \@ranges) if defined($last_seq);

Update: Finally, if the input isn't sorted or if you prefer something simpler (at the cost of using more memory),

my %ranges_by_seq; while (<>) { my ($seq, $p1, $p2) = (split)[0, 2, 3]; ($p2, $p1) = ($p1, $p2) if $p2 < $p1; push @{ $ranges_by_seq{$seq} }, [ $p1, $p2 ]; } for my $seq (keys(%ranges_by_seq)) { my $ranges = $ranges_by_seq{$seq}; system(extractseq => "-sequence=$seq.seq", "-auto", "-stdout", "-separate", "-reg=" . join(',', map { "$_->[0]..$_->[1]" } @$ranges), ) and die("system: $?/$!\n"); }

In reply to Re: Perl Multiline Regex by ikegami
in thread Perl Multiline Regex by joomanji

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.