Missing first line when reading in .csv file

Discussion in 'Perl Misc' started by Dackle, Jan 28, 2005.

  1. Dackle

    Dackle Guest

    I'm having a recurring problem reading in .csv files. The first line
    in the file is always missing, and so when the code below executes,
    $dt[0] will contain the second line, $dt[1] the third, and so on.
    Usually I get around this by manually opening the .csv file in notepad
    and inserting "DUMMY" and a carriage return, then resaving. Then when
    the code executes, DUMMY is skipped, and $dt[0] containes the first
    line, $dt[1] the second etc., which is how I want it. In general, this
    manual process works fine, but I'd rather skip it if possible. Has
    anyone encountered this problem before or knows why it is happening?

    open SCORES, "C:/$filename.csv" or die "can't open file: $!";
    while (<SCORES>) {
    chomp;
    @x=<SCORES>;
    }
    $n=@x+0; # Number of games
    for $a (0..$n) {
    @c=split(",",@x[$a]);
    $dt[$a]=$c[0]; # Date
    $vis[$a]=$c[3]; # Visitor
    $hom[$a]=$c[6]; # Home
    }
     
    Dackle, Jan 28, 2005
    #1
    1. Advertising

  2. Dackle wrote:
    > I'm having a recurring problem reading in .csv files.


    Indeed.
    Why don't you just use Text::CSV or Text::CSV::Simple rather then rolling
    you own code?

    jue
     
    Jürgen Exner, Jan 28, 2005
    #2
    1. Advertising

  3. Dackle

    Guest

    Dackle wrote:
    > I'm having a recurring problem reading in .csv files. The first line
    > in the file is always missing, and so when the code below executes,
    > $dt[0] will contain the second line, $dt[1] the third, and so on.
    > Usually I get around this by manually opening the .csv file in

    notepad
    > and inserting "DUMMY" and a carriage return, then resaving. Then when
    > the code executes, DUMMY is skipped, and $dt[0] containes the first
    > line, $dt[1] the second etc., which is how I want it. In general,

    this
    > manual process works fine, but I'd rather skip it if possible. Has
    > anyone encountered this problem before or knows why it is happening?
    >
    > open SCORES, "C:/$filename.csv" or die "can't open file: $!";
    > while (<SCORES>) {
    > chomp;
    > @x=<SCORES>;
    > }


    hmm.. seems obvious enough...

    the first line you have already moved the file pointer down one line.
    It's as if you said: while ($_ = <SCORES>) { chomp $_ ...
    now you are starting at the second line and then...
    @x=<SCORES>; in scalar context, you have just sucked in the whole file
    into @x starting at the second line. All you had to do was:

    @x=<SCORES;

    without the while.

    > $n=@x+0; # Number of games
    > for $a (0..$n) {
    > @c=split(",",@x[$a]);
    > $dt[$a]=$c[0]; # Date
    > $vis[$a]=$c[3]; # Visitor
    > $hom[$a]=$c[6]; # Home
    > }
     
    , Jan 28, 2005
    #3
  4. Dackle

    Dackle Guest

    > without the while.


    Thanks a lot, it works now. That's been bugging me for a long time.
     
    Dackle, Jan 28, 2005
    #4
  5. Dackle <> wrote:
    > I'm having a recurring problem reading in .csv files. The first line
    > in the file is always missing,



    No it isn't.

    Your code reads the first line and discards it without processing it.


    > while (<SCORES>) {



    That reads the 1st line into the $_ variable.

    Your code never does anything with what is in the $_ variable.

    > chomp;
    > @x=<SCORES>;



    This grabs ALL of the remaining lines, so the while() loop
    will *always* execute exactly one time, it is an UNloop!



    > $n=@x+0; # Number of games



    One-character variable names really suck, have you been
    programming for long?


    > for $a (0..$n) {
    > @c=split(",",@x[$a]);



    You should always enable warnings when developing Perl code!

    A pattern match should *look like* a pattern match.

    Whitespace is not a scarce resource, feel free to use as much
    of it as you like to make your code easier to read and maintain.


    @c = split(/,/, $x[$a]);


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jan 28, 2005
    #5
  6. Dackle

    Anno Siegel Guest

    <> wrote in comp.lang.perl.misc:
    >
    > Dackle wrote:
    > > I'm having a recurring problem reading in .csv files. The first line
    > > in the file is always missing, and so when the code below executes,
    > > $dt[0] will contain the second line, $dt[1] the third, and so on.
    > > Usually I get around this by manually opening the .csv file in

    > notepad
    > > and inserting "DUMMY" and a carriage return, then resaving. Then when
    > > the code executes, DUMMY is skipped, and $dt[0] containes the first
    > > line, $dt[1] the second etc., which is how I want it. In general,

    > this
    > > manual process works fine, but I'd rather skip it if possible. Has
    > > anyone encountered this problem before or knows why it is happening?
    > >
    > > open SCORES, "C:/$filename.csv" or die "can't open file: $!";
    > > while (<SCORES>) {
    > > chomp;
    > > @x=<SCORES>;
    > > }

    >
    > hmm.. seems obvious enough...
    >
    > the first line you have already moved the file pointer down one line.
    > It's as if you said: while ($_ = <SCORES>) { chomp $_ ...
    > now you are starting at the second line and then...
    > @x=<SCORES>; in scalar context, you have just sucked in the whole file
    > into @x starting at the second line. All you had to do was:
    >
    > @x=<SCORES;
    >
    > without the while.
    >
    > > $n=@x+0; # Number of games
    > > for $a (0..$n) {
    > > @c=split(",",@x[$a]);
    > > $dt[$a]=$c[0]; # Date
    > > $vis[$a]=$c[3]; # Visitor
    > > $hom[$a]=$c[6]; # Home
    > > }


    That fixes the particular misbehavior, but the program is still
    unnecessarily roundabout.

    Why read a file into an array, and then pass through it line by line
    anyway? It doesn't make sense. The standard way in Perl to process
    the lines of a file is a while loop (untested):

    my $a = 0;
    while ( <SCORES> ) {
    chomp; # you forgot that, could bite some day
    ( $dt[ $a], $vis[ $a], $hom[ $a]) = ( split /,/)[ 0, 3, 6];
    $a ++;
    }

    The index variable $a isn't really necessary, it is the length of the
    arrays @dt etc. Here is one way to build them directly:

    ( $dt[ @dt], $vis[ @vis], $hom[ @hom]) = ( split /,/)[ 0, 3, 6] for
    map { chomp; $_ } <DATA>;

    Then again, as has been noted, this is really a job for one of the
    csv modules.

    Anno
     
    Anno Siegel, Jan 28, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stu
    Replies:
    4
    Views:
    10,866
    Paul Clement
    Oct 10, 2005
  2. Tintin92
    Replies:
    1
    Views:
    1,813
    Andrew Thompson
    Feb 14, 2007
  3. Sebastian Bassi

    CSV without first line?

    Sebastian Bassi, Jul 15, 2007, in forum: Python
    Replies:
    3
    Views:
    634
  4. scad
    Replies:
    23
    Views:
    1,220
    Alf P. Steinbach
    May 17, 2009
  5. Richard Schneeman
    Replies:
    16
    Views:
    561
    Daniel Bush
    Aug 27, 2008
Loading...

Share This Page