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

why does this script read every other line of a file that goes on and on like this? script: #!/usr/bin/perl open(LIST, ">newlist.lst") or die; #the new classlist select(LIST); #select the newlist for output while (<>) { @chunks = split(/\",\"/ , <>); #split the lines at "," print $chunks5 , " , " , $chunks6 ," , ", $chunks7," , , , ",$chunks4 ," , ", $chunks4 , " , , " ,unpack("A1",lc($chunks7)),lc($chunks6), "\n"; } close(LIST); #we're done! file: "2000","A","AMS","151","01","052708224","Amoroso","Jason"," ","CSE"," "," "," ","U1"," ",3.0 "2000","A","AMS","151","01","107723030","Aprea","Stephen","V","GEN"," "," "," ","U1"," ",3.0 88 students in, 44 out it reads in one format of classlist and outputs another. other than the skipping lines, everything works fine, but if anyone has any suggestions feel free to post them -thanks -Lucas

Replies are listed 'Best First'.
Re: every other line?
by chromatic (Archbishop) on Sep 02, 2000 at 00:10 UTC
    Every time you use the input operator (<>), you read a new line. You can read it into a variable, but you're not doing that, so it goes directly into the special variable $_. You read a line in the while condition, and then clobber it by reading the next line from the file in the split operation.

    Do this instead:

    while (my $line = <>) { @chunks = split(/","/, $line); # other stuff }
    Or use Text::CSV to split up your comma separated values, if your data has complicated stuff like commas within your quotes.
(Ovid) Re: every other line?
by Ovid (Cardinal) on Sep 02, 2000 at 00:22 UTC
    If you post your code in <CODE></CODE> tags, it will format properly.

    Your problem is that you are trying to split on the file specified on the command line since your are splitting the diamond operator (<>). This forces a new read of the file (hence skipping a line). In your case, try splitting on $_ instead. Also, I suggest that you enable warnings (-w) and use strict. These will save you many hours of heartbreak in the long-run. Here's a slight clean-up of your code:

    #!/usr/bin/perl -w use strict; my @chunks; open(LIST, ">newlist.lst") or die; #the new classlist select(LIST); #select the newlist for output while (<DATA>) { @chunks = split(/\",\"/ , $_); #split the lines at "," print $chunks[5] , " , " , $chunks[6] ," , ", $chunks[7]," , , , " +,$chunks[4] ," , ", $chunks[4] , " , , " ,unpack("A1",lc($chunks[7])) +,lc($chunks[6]), "\n"; } close(LIST); #we're done!

    Cheers,
    Ovid

    Update: Of course, once again I post only to see that someone else got there first. I really need to remember to check that.