Hi,
I'm 1 month into learning Perl and done reading "Minimal Perl" by Tim Maher (which I enjoyed enoumously). I'm not a programmer by profession but want to use Perl to automate various tasks at my job. I have a problem (obviously) and are looking for your much appreciated help.
THE TASK:
1) I need to test the records from a text file with fields delimited by semicolon (infile.txt).
2) At the end of the test I need to generate a report containing the tested records (outfile.txt).
3) I want to use the implicit file loop available by the -n invocation option to go about the task in an AWKish way (but using AWK is not an option).
4) By way of the implicit loop the read lines from infile.txt should be pushed into an array.
5) In the END block the contents of the array should be written to the outfile.
My test file (infile.txt) contains 3 records of 3 semicolon delimited fields each:
1;2;3
a;b;c
4;5;6
The script is invoked from the command line by:
c:\perl test.pl infile.txt
My environment is Strawberry Perl, v.5.10.1 on Windows XP.
My stripped down script (stripped of everything irrelevant to the problem) is called test.pl:
#! 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;
}
THE PROBLEM:
Allthough all 3 records from the infile gets processed (a print statement reveals this during processing) only the first record makes it to the array and from the array to the outfile (another print statement and the outfile reveals this).
What am I doing wrong? - pls help!
Best regards,
John
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.