in reply to Passing Array of Arrays
--> you define this as a lexical array for this sub.my @fileanddate;
Then, in the foreach loop, you do:
--> After setting it, you push a reference to @fileanddate onto @filesanddates.@fileanddate = ($filename, $mtime); $filesanddates[$filecounter] = \@fileanddate;
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:
Or better yet:foreach my $filename (@filenames) { my $mtime=(stat ($filename))[9]; my @fileanddate=($filename, $mtime); # Note lexical scoping of @fileanddate push @filesanddates, \@fileanddate; }
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 |