in reply to Extract length sum from columns and print pattern into another file

Try but beware of off-by-1 errors in the lengths and gaps

#!/usr/bin/perl use strict; my $outfile = 'file2.txt'; open OUT,'>',$outfile or die "Could not open $outfile : $!"; my $sum_len = 0; my $sum_gap = 0; my $def; my $end; while (<DATA>){ if (/TIFF[^\d]+(\d+)\.\.(\d+)/g){ # gaps if (defined $end){ my $gap = $1 - $end; $sum_gap += $gap; printf "%5d to %5d = %5d gap\n",$end,$1,$gap; } # lengths my $len = $2 - $1; $sum_len += $len; printf "%5d to %5d = %5d\n",$1,$2,$len; $end = $2; $def = ''; } elsif (/def="([^"]+)/){ (undef,$def) = split ':',$1; } elsif (/pattern="([^"]+)/){ printf OUT ">%s\n%s\n",$def,$1 if $def; } } close OUT; printf " Sum = %5d %5d\n",$sum_len,$sum_gap; __DATA__ XXXXXXXXXX YYYYYYYY TIFF 1..203 /def="Z/AA:XGproxy1" /pattern="gdgdfn6N6" TIFF trans(256..298) /def="Z/AA:ZYprompt5" /pattern="HbgREV5ehe757gAH" TIFF trans(303..323) /pattern="hfftvt&&jdgY=)" XXXXXXXX YYYYYYYY
poj
  • Comment on Re: Extract length sum from columns and print pattern into another file
  • Download Code

Replies are listed 'Best First'.
Re^2: Extract length sum from columns and print pattern into another file
by rebkirl (Acolyte) on Jun 07, 2019 at 15:13 UTC
    Thank you! So, in your code, file1.txt would be DATA, right? How is <DATA> given as input input file?
      my $infile = 'file1.txt'; open IN,'<',$infile or die "Could not open $infile : $!"; while (<IN>){ } close IN;
      See perlopentut

      poj