in reply to foreach on array

I don't understand why you split on new lines here:

while (<APPLIST>) { push @inpfile, [split /\n/]; }

The way you read the file in, you only get one line at a time anyway. Instead of a while loop, you should be able to just do

@inpfile = <APPLIST>;

Later, you do this

local $/; open SLURP, $ifile or die "can't open $ifile: $!"; @filedata = <SLURP>; close SLURP or die "cannot close $ifile: $!";

After you undefined $/, <SLURP> will give you a scalar, so it doesn't seem to make sense to assign it to an array.

Either drop the local $/, or write $whole_filedata = <SLURP>.

Replies are listed 'Best First'.
Re^2: foreach on array
by tirwhan (Abbot) on Oct 13, 2005 at 09:18 UTC

    I was going to reply something similar when I read the original node. But then I started to wonder whether perhaps the OP was dealing with legacy code and a file that used Windows and Unix line terminators interspersed. For example:

    data1\n data2\r\n data3\n ...etc
    If this file were to be read in under Windows in a while loop, the first line would be
    data1\ndata2\r\n
    ,no? (Can't check, I don't have access to a Windows machine.) In which case splitting on \n and then pushing the resulting array into another one ( as suggested by shenme above ) would make perfect sense (albeit a chomp would still be required). Or am I mistaken?
Re^2: foreach on array
by revdiablo (Prior) on Oct 13, 2005 at 14:38 UTC
    I don't understand why you split on new lines here

    I think that might be a clever way to chomp off the newlines without using, er, chomp. The tricky part that makes it really work is that split discards any empty elements at the end of the list. It's actually kind of a neat way to do this without an intermediary variable. Too bad the entire benefit of that is erased by the fact that it's kind of obfuscated. :-)

Re^2: foreach on array
by Anonymous Monk on Oct 13, 2005 at 08:36 UTC
    Thanks, that did the trick, I obviously have a lot to learn