Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

trying to add lines to .csv file...won't go past 2 lines...

Status
Not open for further replies.

spewn

Programmer
May 7, 2001
1,034
i have the following code:

Code:
$cfile = "updatefile.csv";
open (COUNTER, "$cfile");
$count = <COUNTER>;
close (COUNTER);
if ($count eq '') {
$count = qq{Field1,Field2,Field3,\n};
}
$count = $count.qq{$val1,$val2,$val3,\n};
open (COUNTER, ">$cfile");
print COUNTER ("$count");
close (COUNTER);

it works, but it only will print two lines.

this is assuming that the $val1,$val2 and $val3 are dynamic...

any ideas? i think it has something to do with the \n...

- g
 
your code does exactly what you have written it to do. Are you trying to append to an existing file? Use ">>" instead of ">" when opening the file.

Don't do this:

print COUNTER ("$count");

should be written as:

print COUNTER $count;

no parenthesis or quotes.

Are you sure you want a comma then a newline?

$count = $count.qq{$val1,$val2,$val3,\n};

that will leave a trailing comma that's probably not going to be used for anything. Watch how you write code too, don't clump it all up like this:

$count = $count.qq{$val1,$val2,$val3,\n};

that can lead to problems if the context of how the dot is used is not clear.

better written as:

$count = $count . qq{$val1,$val2,$val3,\n};

or even better:

$count .= qq{$val1,$val2,$val3,\n};




------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
The Text::CSV module is simple to use and takes a lot of the gotchas out of manipulating CSV files, both for input and output.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
good point Steve. Text::CSV_XS is a faster version of the same.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top