Hi Lauri,
Good to hear -- if the file is small enough to be read in entirely, that
is what I would do. If you need to add in another loop to output a
third file, then you can just add it in easily to make another pass.
One suggestion which makes no difference to your program and some people
might disagree with me is to move the "close" up so that you close the
file pointer as soon as you know you don't need it anymore. I use to
close it at the end, but when my scripts became pages long, I sometimes
forgot... :-) And it minimizes the time that you keep it open. But it
won't make a big difference for your script...just a suggestion.
Ray
Lauri Nikkinen wrote:
Thanks to Ray, I ended up doing this in this kind of way:
-L
#==================================
#!/bin/perl
use warnings;
use strict;
use Text::CSV_XS;
use Tie::Handle::CSV;
my $fh = Tie::Handle::CSV->new(csv_parser => Text::CSV_XS->new({binary => 1}),
file => 'rek.csv',
header => 1);
#Read the data into an array
my @data = <$fh>;
my $csv_line;
#Write into the first file
my $outfile = 'csvtmp.csv';
open (OUTFILE, ">", $outfile) or die $!;
foreach $csv_line (@data) {
print OUTFILE $csv_line->{'Name'} . ": " . $csv_line->{'Surname'} . "\n";
}
#Write into the second file
my $outfile2 = 'csvtmp2.csv';
open (OUTFILE2, ">", $outfile2) or die $!;
foreach $csv_line (@data) {
if ($csv_line->{'Company'} =~ m/Middle./) {
print OUTFILE2 $csv_line->{'Surname'} . ": " .
$csv_line->{'Company'} . "\n";
}
}
close OUTFILE;
close OUTFILE2;
close $fh;
#==================================
2008/11/18 Raymond Wan <[EMAIL PROTECTED]>:
Hi Lauri,
Lauri Nikkinen wrote:
I would like to parse a .csv file and write certain records into two
separate files. The program below writes the records easily into the
csvtmp.csv but the second file is only created with no records
written. I cannot see the problem here, the second file is produced
easily if the first loop is discarded. Any ideas? Thanks.
-L
======================================
#!/bin/perl
use warnings;
use strict;
use Text::CSV_XS;
use Tie::Handle::CSV;
my $fh = Tie::Handle::CSV->new(csv_parser => Text::CSV_XS->new({binary =>
1}),
file => 'rek.csv',
header => 1);
#Write into the first file
my $outfile = 'csvtmp.csv';
open (OUTFILE, ">", $outfile) or die $!;
while (my $csv_line = <$fh>) {
print OUTFILE $csv_line->{'Name'} . ": " .
$csv_line->{'Surname'} . "\n";
}
From what I can tell, in your first loop, you are reading in from the input
file and exhaust the input file (reached End-of-File [EOF]) to leave this
while loop. So, when you enter the second while loop (which comes after and
I have cut), the input file has been exhausted and there is nothing to do.
if you want to read again with the second while loop, you can either:
1) close the file handle ($fh) and re-open it
2) Seek to the beginning of the file between the two while loops
(http://perldoc.perl.org/functions/seek.html)
3) Open it once, read all the lines into a buffer and then use that buffer
(an array, etc.) to output it to your two output files.
And I'm sure there are many other options available...
Ray
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/