Perl does have a very fine regex, as demonstrated below by
BrentDax, which is what I was thinking of doing initially, forgetting, at the time, that feature of
split().
No matter how you slice it, though, regex or otherwise, you should put the code into a sub-routine. At least, anyone who is concerned about maintainability would make sure to do that. Having a compact regex is not a license to cut and
paste it all over your code, wherever it is required.
If you're afraid of this code looking like Java, well, that isn't too hard to fix:
my %time_value = ( d => 86400, h => 3600, m => 60, s => 1 );
sub string_to_time
{
my ($time) = @_;
my $value = 0;
map { $value += $1*$time_value{$_} if ($time =~ s/(\d+)$_//) }
+ sort keys %time_value;
return ($value && !length $time)? $value : undef;
}
sub time_to_string
{
my ($time) = @_;
my $value = '';
(defined $time && $time > 0) || return undef;
map { $value .= int($time/$time_value{$_}).$_ and $time %= $ti
+me_value{$_} if ($time > $time_value{$_})} sort keys %time_value;
return $value;
}
So much for readability and maintainability though.
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.