I'm developing a Perl script that will perform certain functions based upon the time of day and day of the week. Below is a minimal test case of a piece of code that I'm working on.
#!/usr/bin/perl -w
use strict;
my $now = 8; # we'll pretend it's between 8 and 9 PM
my @hours;
my %url = (
monday => {
1 => "Too early in the morning",
3 => "Someone came home from a bender",
7 => "Time for coffee",
16 => "Need to slack off the last hour of work"
}
);
# begin awkward code
@hours = reverse (sort {$a <=> $b} keys %{$url{"monday"}});
foreach (@hours) {
$now = $_, last if $_ <= $now;
}
# end awkward code
That code will assign "7" to
$now. It seems to me that there should be a more "perlish" way to extract from the hash of hashes the greatest hash key that is less than or equal to $now. Can anyone help me come up with a clear example? (On the other hand, I don't want terribly obfuscated code).
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.