in reply to Push records to array during implicit loop and write to file
Every time you read a line you @result is empty. The implicit loop is best left to oneliners.BEGIN { $^W = 1; } BEGIN { $/ = "\n"; $\ = "\n"; } LINE: while (defined($_ = <ARGV>)) { chomp $_; our(@F) = split(/;/, $_, 0); use strict 'refs'; { my $outfile = 'outfile.txt'; my @result; my $var1 = $F[0]; my $var2 = $F[1]; my $var3 = $F[2]; push @result, "$var1 $var2 $var3"; print "Processed infile line no.: $."; sub END { print 'Array contains: ' . @result . ' element(s).'; writeToFile(\@result, $outfile); } ; } sub writeToFile { my $array_ref = $_[0]; my $filename = $_[1]; die unless open OUTFILE, "> $filename"; foreach $_ (@{$array_ref;}) { print OUTFILE $_; } close OUTFILE; } ; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
[SOLVED] Re^2: Push records to array during implicit loop and write to file
by jospan (Novice) on Jan 12, 2010 at 13:11 UTC | |
by Marshall (Canon) on Jan 12, 2010 at 20:02 UTC | |
by jospan (Novice) on Jan 12, 2010 at 22:42 UTC | |
by Marshall (Canon) on Jan 13, 2010 at 00:39 UTC |