First thought on reading this is "wow, you're doing a lot that you don't have to." There are a few stylistic issues that boil down to making your code a little more readable. But a big thing might be to forego the idea of using an array of arrays. You're dealing with filenames, which, in a given directory, must be unique. You want to record a certain bit of information about each file, so why not have the keys be the filenames and the values be those bits of information?

For example, to get the mtimes on all your files, which is the only bit from the stat call you want to keep, you can do the following:

my %filenames =(); foreach( <post*.*> ) { $filenames{$_} = -M $_; }
This uses the -M filetest operator (see perldoc -f -X on your system for more info on the filetest operators) instead of that long stat call and is more mnemonic than using, say, a list slice on the stat call. ( as an aside you can ignore for the moment, you could compress to a single line with my %filenames = map { $_ => -M $_ } <post*.*>; this does pretty much the same thing as the foreach loop, but is shorter). Anyhow, with a hash, you would sort it by the mod time via something like the following:
my @sorted_keys = sort { $filenames{$a} <=> $filenames{$b}} keys %file +names;
(you don't need to name the array, but I just wanted to illustrate how you get the set of keys of the filename hash sorted according to values).

Now, for your original question (finally!). I think what's going on is a scoping issue. You define @fileanddate outside the loop, and put a reference to it into the array you're returning each time you process a file. But there is only one @fileanddate, so you're filling up your returned array with a bunch of references to that same thing. A better approach, keeping your current code for the most part, would have your loop that constructs the array look like this:

foreach my $filename ( @filenames ) { # create an ANONYMOUS array (basically a reference to an array) my $record = [ $filename, -M $filename ]; push @filesanddates, $record; }
This ought to do what you want with much less fuss. An absolutely minimal fix would just move the declaration of @fileanddate into the subroutine (update NO! array building loop ... ooops. ), so you get a "fresh" lexically-scoped variable on each iteration through the loop.

HTH!

If not P, what? Q maybe?
"Sidney Morgenbesser"


In reply to Re: Passing Array of Arrays by arturo
in thread Passing Array of Arrays by marctwo

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.