in reply to Adding an array to an existing csv file as a new column
Hello c.con, and welcome to the Monastery!
haukex has shown you the correct way to process CSV files using Text::CSV. I just want to comment on your style of variable declarations.
(1) For Perl versions 5.12.0 and above, use VERSION; enables use strict; — but unfortunately your use 5.10.0; does not. The absence of an explicit use strict at the head of your script makes it difficult to catch typos. In particular, the line push @arr, @col2; references the variable @arr which is not declared or used elsewhere in the code. This is likely a typo for push @array, @col2; — an error which use strict; would have caught for you.
(2) It is good practice to declare each variable as near as possible to its point of first use. So, instead of:
my ($fh1, $fh2, $fh3, $fh4, $fh5); ... open ($fh1, '<', $file1) or die $!;
just declare $fh1 where it is first used:
open (my $fh1, '<', $file1) or die $!;
If you edit your code according to this principle you will see that the variables $fh5, @col1, @col3, and $lines3 are never used. More importantly, you will immediately see that between the declaration and first use of $fh4 this filehandle is never, in fact, opened for writing!
Programming is hard enough already; why code in a style that makes it harder than it needs to be? ;-)
Hope that helps,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
|
|---|