in reply to Passing Array of Arrays

First you do, in sub GetLogFilesAndDatesIntoArray :
my @fileanddate;
--> you define this as a lexical array for this sub.

Then, in the foreach loop, you do:

@fileanddate = ($filename, $mtime); $filesanddates[$filecounter] = \@fileanddate;
--> After setting it, you push a reference to @fileanddate onto @filesanddates.

That's your error. All of the references you push onto @filesanddates point to the same array, @fileanddate. Hence, all of these point to the same piece of data. This piece of data is the one you last put into it - the last set of (filename, mtime).

To solve this, just make sure that the reference you push onto @filesanddates is to something that is scoped locally to the foreach block. For example:

foreach my $filename (@filenames) { my $mtime=(stat ($filename))[9]; my @fileanddate=($filename, $mtime); # Note lexical scoping of @fileanddate push @filesanddates, \@fileanddate; }
Or better yet:
foreach my $filename (@filenames) { push @filesanddates, [ ( $filename, (stat ($filename))[9] ) ]; }

For more information on scoping, have a look at this tutorial.

CU
Robartes-

Replies are listed 'Best First'.
Re: Re: Passing Array of Arrays
by marctwo (Acolyte) on Mar 11, 2003 at 14:53 UTC
    Thank you thank you thank you all!! I can't believe the speed and quality of these responses. This is truely an enlightened forum :)

    I followed Robartes suggestion of lexical scoping and it works now. This seemed to be the simplest modification to my existing code and something I understood right away. However, I shall certainly read through all the excellent suggestions and try to understand and incorporate them soon.