You have a rather restrictive set of requirements. The implicit loop, using these other options like "naF" as well using an END block as you described aren't usually seen.

I guess it is possible that this is some homework assignment. If it is, then I think this is not a very good one because it uses some features that just aren't used that often.

A more typical thing would be like shown below. There would be some way to get a "usage statement" if the input command was wrong. The input/output "opens" would be done near the beginning of the program. You don't want to have a program that runs a long time, then bombs because the output permissions or path to the output file means that no result can be output!

If you want to process multiple files, I show a simple foreach loop below that will do that. There is no need to close a filehandle before you open it to another file.

Below I show "my @result" just needs to be declared at a higher scope that the input loop. An "our" variable is usually used for situations where a variable needs visibility outside the package it was defined in, which is not the case here.

Anyway, if your requirement statements aren't driven by a homework assignment, I would do something a lot more straightforward.

#! usr/bin/perl -w use strict; if (@ARGV == 0) { print "usage: perl test.pl infile [infile]\n"; exit(1); } my $outfile = "outfile.txt"; open (OUTFILE, '>', $outfile) or die "unable to open $outfile : $!\n"; my @result; foreach my $infile (@ARGV) { open (INFILE, '<', $infile) or die "unable to open $infile : $!\n"; while (<INFILE>) { chomp; my (@vars) = split(/;/,$_); push @result, "@vars"; print "Processed $infile line no.: $. vars= @vars\n"; } } print "Result Array contains: " . @result . " element(s).\n"; writeToFile( \@result); sub writeToFile { my $array_ref = shift; foreach ( @{ $array_ref } ) { print OUTFILE "$_\n"; } } __END__ infile.txt: 1;2;3 a;b;c 4;5;6 infile2.txt: X;Y;Z 9;10;11 C:\TEMP>perl newbie3.pl infile.txt infile2.txt Processed infile.txt line no.: 1 vars= 1 2 3 Processed infile.txt line no.: 2 vars= a b c Processed infile.txt line no.: 3 vars= 4 5 6 Processed infile2.txt line no.: 4 vars= X Y Z Processed infile2.txt line no.: 5 vars= 9 10 11 Result Array contains: 5 element(s). C:\TEMP>cat outfile.txt 1 2 3 a b c 4 5 6 X Y Z 9 10 11

In reply to Re: [SOLVED] Re^2: Push records to array during implicit loop and write to file by Marshall
in thread Push records to array during implicit loop and write to file by jospan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.