I'd suggest two small changes. First, don't stat just your $_ variable - since you are doing an opendir, only the names of the files are returned, not the full path. In this particular case, it is okay, but something to get in the habit of so it does not nail you later. Second, you can simply swap the order of the sort arguments instead of using a reverse. Not a big deal, but a little more compact and readable (IMO). I kept the nice map idea from agoth and also added another argument, in case you feel like sorting by somthing else.

my $dir = shift || "."; my $field = shift || 10; opendir(DIRH, $dir) or die "Cannot open $dir: $!"; my %file = map { $_ => (stat("$dir/$_"))[$field]} grep { !/^\.\.?$/ && /pm$/ } readdir DIRH; closedir(DIRH); for (sort { $file{$b} cmp $file{$a} } keys %file) { print "$_ , $file{$_} \n"; }

In reply to Re: Directory Listing by turnstep
in thread Directory Listing by agoth

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.