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/