Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

rewinding filehandle/line skipping ?

by Anonymous Monk
on Jun 10, 2002 at 10:04 UTC ( [id://173062]=perlquestion: print w/replies, xml ) Need Help??

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

hello,
I have written to a file and would like to then read from the top
of that file. How do I rewind a file to the beginning ?
Also I have a problem with a while loop...here is the code
while (<DATA2>) { $line=<DATA2>; print $line; @array = split(/ /, $line); #split the line up into array s +o [0] is the id and [1] indel 1 etc. #print $array[0]; #print "\n"; foreach $indel (@array) #look at each indel in turn { next if $indel=~/>/; #skip the id @character = split(//, $indel); #look a character at a time foreach $char (@character) { if ($char=~/-/) { #if there is a gap add 1 to $count $count++; } } if($go==0) #if id hasnt been printed yet { print DATA3 "$array[0]"; print DATA3 "\n"; $go++; } @letter = qw(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z); + #converting numbers into letters A=0 etc. print DATA3 "$letter[$count]"; $count=0; } print DATA3 "\n"; $go=0; }
I am reading through my DATA2 file and printing to DATA3.
The problem is, for some reason the program is skipping every other line in DATA2. (I have checked DATA2 is what I expected it to be). At the print $line command only every other line is printed. Any ideas why ?

Thanks

Replies are listed 'Best First'.
Re: rewinding filehandle/line skipping ?
by Molt (Chaplain) on Jun 10, 2002 at 10:07 UTC

    To rewind a file to beginning use seek FILE,0,0. Read the seek for more info on how this works.

    Update:Also spotted why you're losing data.. the while (<DATA2>) reads in one line itself into $_, and then you read in a second into $line with $line= <DATA2>, with the result that half the lines are entirely ignored. A better way would be while (my $line= <DATA2>) instead of either of both of the other lines.

Re: rewinding filehandle/line skipping ?
by Zaxo (Archbishop) on Jun 10, 2002 at 10:19 UTC
    The problem is, for some reason the program is skipping every other line in DATA2.

    The problem is that you are reading two lines, but working on only one of them, replace

    while (<DATA2>) { $line=<DATA2>;
    with:
    while ($line = <DATA2>) {
    I think your code would benefit from use strict; and some lexical scoping. The pronoun $_ instead of $line might help too.

    After Compline,
    Zaxo

Re: rewinding filehandle/line skipping ?
by jmcnamara (Monsignor) on Jun 10, 2002 at 10:34 UTC

    Here is a short example of writing to a file and rewinding it for reading. Note the +> in the open:
    #!/usr/local/bin/perl -w use strict; open MYFILE, "+>somefile.txt" or die "Error message here: $!\n"; # Write to the file print MYFILE "These are the days\n"; print MYFILE "and these are the days\n"; # Rewind the file for reading seek(MYFILE, 0, 0); while (<MYFILE>) { print $_; } close MYFILE;

    See also perlopentut.

    --
    John.

Re: rewinding filehandle/line skipping ?
by Anonymous Monk on Jun 10, 2002 at 10:19 UTC
    Thanx - that worked a treat !
Re: rewinding filehandle/line skipping ?
by cybear (Monk) on Jun 10, 2002 at 10:48 UTC
    Anonymous Monk, you could also close(file) then open(file) again.

    and as for the "while loop loosing data" thing... I had that problem initially also. :-)

Re: rewinding filehandle/line skipping ?
by cjf (Parson) on Jun 10, 2002 at 11:59 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://173062]
Approved by davis
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2024-04-26 01:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found