sugar has asked for the wisdom of the Perl Monks concerning the following question:

Well, i know this is a simple task. But am getting some unwanted lines while printing the results. The length is based on the string below every IDnumber. The length given in the input file is ignored and hence it is calculated freshly from the string itself. Please check out.
input file: ---------- >IDnumber1 length=350 AGCTG >IDnumber2 length=350 AGAACGT >IDnumber3 length=350 AGC expected output file: ---------------------- >IDnumber1 length=5 AGCTG >IDnumber2 length=7 AGAACGT >IDnumber3 length=3 AGC code: ----- #!/usr/bin/perl open FH,"inputfile"; while(<FH>){ $head=();$str=(); if($_=~m/^>/){ ($head,$x)=split(' ',$_); } else{ $str=$_; chomp($str); $len=length($str); } print "$head Length=$len\n"; print "$str\n"; }
please let me know where i am going wrong !!! P.S: i have not included use strict and warnings;

Replies are listed 'Best First'.
Re: printing the length of string variables
by prasadbabu (Prior) on Jan 08, 2009 at 07:25 UTC

    Hi sugar,

    If I understood your requirement correctly, the below coding which is slightly modified form of yours will work. But you can do the same in more simple way.

    use strict; use warnings; my ($head); while(<DATA>){ my ($x, $str, $len, $s); $s = $_; if($s=~m/^>/){ chomp($s); ($head)=(split(/ /,$s))[0]; } else{ chomp($s); $len=length($s); } print "$head length=$len\n$s\n" if($s !~m/^>/); } __DATA__ >IDnumber1 length=350 AGCTG >IDnumber2 length=350 AGAACGT >IDnumber3 length=350 AGC output: ------- >IDnumber1 length=5 AGCTG >IDnumber2 length=7 AGAACGT >IDnumber3 length=3 AGC

    Prasad

      Oh yea, your right. I just realized it :) Thanks for the help.
Re: printing the length of string variables
by chromatic (Archbishop) on Jan 08, 2009 at 07:28 UTC

    You're printing information for every line of the file, even if you haven't yet seen a line for which you want to print the length. You should instead read a line for the ID number and length, then read a line for the base sequence, and only then print.

    If that doesn't work, post your next attempt and this time use strict and warnings, and it'll be easier to help you.

Re: printing the length of string variables
by ikegami (Patriarch) on Jan 08, 2009 at 07:27 UTC
    The main problem is that print "$head Length=$len\n"; is being executed for every line in the file. It needs to be conditional. prasadbabu showed how.
Re: printing the length of string variables
by ikegami (Patriarch) on Jan 08, 2009 at 12:21 UTC
    The solutions provided seem very complex. Here's something simpler:
    #!/usr/bin/perl use strict; use warnings; for (;;) { my $head = <>; my $body = <>; last if !defined($body); chomp( my $seq = $body ); my $len = length($seq); $head =~ s/(length=)\d+/$1$len/; print($head, $body); }

    Update: Length was off by one because of lack of chomping. Fixed. Thanks graff.

Re: printing the length of string variables
by sanku (Beadle) on Jan 08, 2009 at 11:38 UTC
    hi, Try out this one.
    #!/usr/bin/perl open FH,"inputfile"; while(<FH>){ $head=();$str=(); if($_=~m/^>/){ ($head,$x)=split(' ',$_); $head=~s/length=\d+\n+\s+//g; print $head; } else{ $str=$_; chomp($str); $len=length($str); print " Length=$len\n"; print "$str\n"; $len=(); } }