Mail WebOn AS wrote:
> 
> Was looking through the PGAS code and ran into these lines :
> 
> # Is there a better way to do this? Probably
> my $day  = int($time_left/(60*60*24));
> my $hour = int(($time_left%(60*60*24))/(60*60));
> my $min  = int((($time_left%(60*60*24))%(60*60))/60);
> my $sec  = int((($time_left%(60*60*24))%(60*60))%60);
> my $day_hour_min_sec = "$day days, $hour hours, $min minutes, $sec seconds";
> 
> Here is a shorter way :
> 
> my @t = map { $time_left = ($time_left - ($time = $time_left % $_)) / $_; $time 
>}(60,60,24);
> my $day_hour_min_sec = "$time_left days, $t[2] hours, $t[1] minutes, $t[0] seconds";
> 
> but how about the plural s in day / days etc.
> here is what i hacked together.
> 
> my $t = $time_left;
> my @t=qw{day hour minute second};
> my $day_hour_min_sec = join', ',map{"$_ $t[$i++]".($_!=1?"s":"")}reverse 
>map{$t=($t-($,=$t%$_))/$_;$,}(60,60,24,9);
> 
> but in true golf style. anyone for a clever / shorter way of doing that ?
> this one only works for < 9 days, but a Perl golf contest usually last < 9 days.
> 
> Terje K

I generally strip the plural 's' off all units at once with:

  s/\b(1 \w+)s/$1/g


(BTW: golf your  ($_!=1?"s":"")  to  ($_!=1&&"s")  :)

-- 
Rick Klement

Reply via email to