Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2024-04-19 14:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found