Hi guys, I'm trying to create a script that parses the original csv file prints out information into a temporary file and then replaces the original file with a temp one.

So original data look something like:

1,6064.86,85391.25,593.75,13.25 2,6072.17,85392.95,593.79,13.29 3,6078.94,85393.05,593.76,13.26 4,6085.51,85392.22,593.77,13.27

and so on. I need to insert two lines at the top, two lines at the bottom, switch columns 2 and 3 around and replace column 5 with column 1.I'm using Text::CSV for parsing and also Lava GUI package. So part of the code that does parsing looks like this:

my $csv = Text::CSV->new(); open(OLD, '+<', $file) or die Lava::Message("Can't open original file" +); while (<OLD>) { next if ($. == 1); if ($csv->parse($_) { my @columns = $csv->fields(); # open new file for editing open (TEMP, '>', $temp) or die Lava::Message("Can't open tem +porary file"); # extract pattern id from file name my $pattern = substr $file, -12 , 8; #date formatting doesn't work properly so i'll get the date +from user my $select_date = ' '; my $date_panel = new Lava::Panel; $date_panel->text(' '); $date_panel->text(" Script Version: $Version"); $date_panel->text(' '); $date_panel->text(" SITE: YANDI"); $date_panel->text(' '); $date_panel->item("Type the date in format DD-Mon-YY: ", \$s +elect_date, 9); $date_panel->execute('Date Selection') or return 0; #now print first two lines into the TEMP file print TEMP "$pattern ,"; print TEMP "$select_date"; print TEMP ",,Dist=Metres\n"; print TEMP "0,0.000,0.000,0.000,0.000,0.000,0.000\n"; while( <OLD> ) { print TEMP $_; last if $. % 1; } # print the parsed body of old file print TEMP "$columns[0], $columns[2], $columns[1], $columns[ +3], $columns[0]\n"; } } # insert new lines at the end print TEMP "\n0, 0.000, 0.000, 0.000,\n"; print TEMP "0, 0.000, 0.000, 0.000, END\n"; close TEMP; close OLD; copy $temp, $file; #now we delete the temporary file Lava::Show("Deleting temporary file"); unlink $temp or Lava::Message "Couldn't delete the temporary file!"; END;

So far everything works mostly fine and the i get my columns in correct order but for some reason it only prints the 2nd line of rearranged data and then moves on to insert text at the bottom. Anyone knows where I'm doing something wrong?


In reply to Parsing CSV only returns the second line of the file by saint_geser

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.