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

hi,
I am a baby at perl so pls be gentle.
I am tring to read and write to a file at the same time. for now I have separated them (read from TEMPFILE and write to DBFILE).
what I need to do is add to the end of each line in the temp file the value $sequenceValue.
another problem Im having is althogh I use "seek" when reding the file I get the second line and not the first. thanks.
print "what length?\n"; $sequenceLength = getc; $fileName = $sequenceLength . "temp.txt"; open(TEMPFILE,"+>".$fileName) or die "Can't open output.txt: $!"; sub builed_list{ my($length, $seq)=@_; if($length == 1) { print TEMPFILE $seq; print TEMPFILE "\n"; return; } $length--; $sequenceA = $seq ."A"; builed_list($length, $sequenceA); $sequenceC = $seq ."C"; builed_list($length, $sequenceC); $sequenceG = $seq ."G"; builed_list($length, $sequenceG); $sequenceT = $seq ."T"; builed_list($length, $sequenceT); } $sequence = "A"; &builed_list($sequenceLength,$sequence); $sequence = "C"; &builed_list($sequenceLength,$sequence); $sequence = "G"; &builed_list($sequenceLength,$sequence); $sequence = "T"; &builed_list($sequenceLength,$sequence); $seekGood = seek TEMPFILE, 0, 0; #go to the begining of the file if($seekGood == 0) { die "can't seek\n"; } $fileName = $sequenceLength . "DB.txt"; open(DBFILE,"+>".$fileName) or die "Can't open output.txt: $!"; while(<TEMPFILE>) { read TEMPFILE, $sequence, $sequenceLength; $sequenceValue = 0; for ($i=0; $i<$sequenceLength; $i++) { $base = substr $sequence, $i, 1; $addValue = 0; if($base eq "C") { $addValue += 1; } elsif($base eq "G") { $addValue += 2; } elsif($base eq "T") { $addValue += 3; } for($j=$i+1; $j<$sequenceLength; $j++) { $addValue *= 10; } $sequenceValue += $addValue; } print DBFILE "$sequence $sequenceValue"; print DBFILE "\n"; printf("%s %d\n",$sequence, $sequenceValue); }

Replies are listed 'Best First'.
Re: how to read and write at the same time + a problem with seek
by Abigail-II (Bishop) on May 07, 2004 at 23:55 UTC
    what I need to do is add to the end of each line in the temp file the value $sequenceValue.
    You can't do that by opening a file for reading and writing, and processing the file line by line. If you read a line, increase its length, and write it back, you will have (partially) overwriten the next line. The file system has no concept at all of lines. Files are just sequences of bytes.
    another problem Im having is althogh I use "seek" when reding the file I get the second line and not the first. thanks.
    That's because you read in the first line with <TEMPFILE>, discard its result, and then read something with read.

    Abigail

Re: how to read and write at the same time + a problem with seek
by eXile (Priest) on May 08, 2004 at 00:28 UTC
    If I understand your question correctly perl -i might also do the trick (see perlrun for details, or type perldoc perlrun on your machine if www.perldoc.com is down):
    perl -i -p -e ' BEGIN { print "what length?\n"; $sequenceLength = getc; } s/$/$sequenceLength/' seq.txt

    will print the same question you asked for and add it to all lines in the file seq.txt.

    Note that your getccall will only fetch 1 character, so if I answer '33' to the first question, it will only add a '3'. You might want to replace the getc part with something like:

    $sequenceLength = <STDIN>; chomp($sequenceLength);