Converting The "Next Line" Into "First Line " In A Perl Loop
1
0
Entering edit mode
11.6 years ago
rehma.ar ▴ 290

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.5k views
ADD COMMENT
0
Entering edit mode
11.6 years ago
Vikas Bansal ★ 2.4k

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)
ADD COMMENT

Login before adding your answer.

Traffic: 1569 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6