Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

A newbie to Perl question.

I have a list file consisting of a number of records e.g.

aaaa
bbbb
cccc
dddd

I do this to get them into an array

open APPLIST, $applistfile or die "Can't open $applistfile file: $!"; while (<APPLIST>) { push @inpfile, [split /\n/]; } close APPLIST or die "Cannot close $applistfile: $!";
I now wish to do some work with each element in the list like this

foreach my $ifile (@inpfile) { print STDOUT "\nFile $ifile being applied\n"; # First read the contents of the file into an array { local $/; open SLURP, $ifile or die "can't open $ifile: $!"; @filedata = <SLURP>; close SLURP or die "cannot close $ifile: $!"; } # split the data into each statement (i.e. between go's) my @filedata_new = (); foreach(@filedata) { push @filedata_new, split('go', $_); # split on the go } # now pass each statement to the opened connection and execute + it foreach my $statement (@filedata_new) { chomp($statement); $statement=~s/^\s+//; #print STDOUT "The statement being executed is $statem +ent\n"; my $sth = $dbh->prepare("$statement"); $sth->execute; } }
The problem is that instead of $ifile being aaaa,bbbb etc..
I get this instead

File ARRAY(0x11e6e8) being applied can't open ARRAY(0x11e6e8): No such file or directory

What am I doing wrong ?

Replies are listed 'Best First'.
Re: foreach on array
by rduke15 (Beadle) on Oct 13, 2005 at 08:22 UTC

    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>.

      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?
      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. :-)

      Thanks, that did the trick, I obviously have a lot to learn
Re: foreach on array
by shenme (Priest) on Oct 13, 2005 at 07:46 UTC
    Try it without the '[' and ']'?
    push @inpfile, split /\n/;

    The '[' and ']' surrounding the split put the result into an "anonymous array", then you push the reference to the anonymous array onto @inpfile. Later when you print one of those values you see 'ARRAY(...)', which it is, an array reference.

    Were you perhaps thinking you wanted
    push @inpfile, ( split /\n/ );

Re: foreach on array
by Rajeshk (Scribe) on Oct 13, 2005 at 08:50 UTC
    Hi,
        From your scrip : push @inpfile, [split /\n/];
    
        The wrong is: 
        Split function return a list. Then the list is assign to array element.
        
    
    Try this one
    
    while (<APPLIST>) {
            chomp;
    	push @inpfile, $_;
    }
    
    or simply
    
    @inpfile = <APPLIST>; #store each line from a file into array