jospan has asked for the wisdom of the Perl Monks concerning the following question:
The script is invoked from the command line by:1;2;3 a;b;c 4;5;6
THE PROBLEM:#! usr/bin/perl -wlnaF';' use strict; { my $outfile = "outfile.txt"; my @result; # Assign fields to variables my $var1 = $F[0]; my $var2 = $F[1]; my $var3 = $F[2]; # Push fields into an array push @result, ( "$var1 $var2 $var3" ); # This debug print shows all 3 infile lines were processed print "Processed infile line no.: $."; END { # This debug print shows only 1 element in array print "Array contains: " . @result . " element(s)."; # pass array by reference and filename by value writeToFile( \@result, $outfile ); } } sub writeToFile { # Takes a reference to an array and a filename as input # Writes the contents of the array to the file my $array_ref = $_[0]; my $filename = $_[1]; open OUTFILE, "> $filename" or die; foreach ( @{ $array_ref } ) { print OUTFILE; } close OUTFILE; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Push records to array during implicit loop and write to file
by Anonymous Monk on Jan 12, 2010 at 01:15 UTC | |
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 |