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?
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.