What are you doing there?
foreach $line (@array) { $line =~ s/\s{1}//g; }

You are aliasing $line to the elements of @array; but because you emptied it just before, the loop never executes at all. Even if it did, you would be substituting on the array's elements; but what you want is to do so on the line of input data.

I'll quote from perldoc -f split:

As a special case, specifying a PATTERN of space (' ') will split on white space just as split with no arguments does. Thus, split(' ') can be used to emulate awk's default behavior, whereas split(/ /) will give you as many null initial fields as there are leading spaces. A split on /\s+/ is like a split(' ') except that any leading whitespace produces a null first field.

In other words,
just @array = split (" ", $line);
and you should be fine.
(And it rhymes, too.)

I'd like to mention you should rather move the my @array; into the loop - instead of emptying the array every time over. Variables should be declared as close to where they're used as possible, and restricted to as narrow a scope as feasible.

Taking all this together and using the fact that you can perfectly well work with $_ without assigning it to something first, we get this:
#!/usr/local/bin/perl -w use strict; open (FH, $ARGV[0]) or die "unable to open input file: $!"; # more hel +pful message open (OUTFILE, ">$ARGV[1]") or die "unable to open output file: $!"; # + this should have one too while (<FH>){ chomp; my @array = split; print OUTFILE "@array\n"; # or whatever has to be done }

As a general coding style, I would propose you don't try to handle the filenames yourself; let Perl decide about the input arguments and let the user decide, via redirection, where he wants to put the stuff. So we get this:
#!/usr/local/bin/perl -w use strict; while (<>){ chomp; my @array = split; print "@array\n"; # or whatever has to be done }

A sidenote for the curious: perl -na will build almost exactly that loop framework for you. :)

Makeshifts last the longest.


In reply to Re: problems with arrays by Aristotle
in thread problems with arrays by Anonymous Monk

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.