This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Converting The "Next Line" Into "First Line " In A Perl Loop

hello every one,

i am learning perl scripting here is is a tab delimited file i want to parse

chromosome position1 position2

chromosome position11 position22

chromosome position111 position222

the result i want to have is like that

chromosome position1 position11 (position1-position11 )

chromosome position11 position111 (position11-position111 )

the script i have now is something like this

 > open file while $line = <file> { 
    chomp ; 
    chromo, pos1, pos2 = split("\t",$line);
    $nextline =  <file>
    chomp;
    chromo1, pos11, pos22 = split("\t",$line);
    $dif = (pos1 - pos11);
    print pos1, pos11 , $dif 
    line = nextline ;
        }
close file;

but the result i got is like

chromosome position1 position11 (position1-position11 )

chromosome position111 position1111 (position111-position1111 )

can anyone help me getting my required result.

perl

1 answer

I am not sure if I understood your input and output correctly. Perhaps, you can use -

open $first, '<',$ARGV[0] or die "Unable to open input file: $!";
$_ = <$first>;
($pc, $ps, $pe) = split; # previous entry
while (<$first>) {
         ($cc, $cs, $ce) = split; # current entry
         print $pc, "\t",$ps,"\t",$cs,"\t",($ps - $cs),"\n";
        ($pc, $ps, $pe ) = ($cc, $cs, $ce);
}
perl test.pl input.file

If you want to do it only if chromosome is same, then add if condition before print-

if ($pc eq  $cc)

Log in to answer this question.