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; } ; }
In reply to Re: Push records to array during implicit loop and write to file
by Anonymous Monk
in thread Push records to array during implicit loop and write to file
by jospan
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |