If all files in the directory are newer than the beginning of the script execution, you'll get no results. That's why you shouldn't initialize your "oldest" to 0, but rather to the first element of the array, if any:
my @names = glob "*" or die "Nothing to scan";
my $oldest_name = shift @names;
my $oldest_age = -M $oldest_name;
for (@names) { # remaining names
if (-M > $oldest_age) {
$oldest_name = $_;
$oldest_age = -M _;
}
}
printf "%s is %.2d days old\n", $oldest_name, $oldest_age;
Why does this look familar? Why, it's one of our
Llama exercises!
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.