R. Joseph Newton wrote:
dan wrote:


I have a string, which is to represent a length of time, say 3d4h45m12s
which can be variable, so you may end up with just 3m, or 5h2m, etc. What I
need to do, is to take this string, and split it up into $days, $hours,
$minutes etc, and then create that amount of time in seconds, which will
then be added to the current timestamp to create an expiry time.

Try:

#!/usr/bin/perl -w

use strict;
use warnings;
my %timeHash;

my $str = "3d4h45m12s";
if ($str =~ /(\d+)d(\d+)h(\d+)m(\d+\.?d*)/) {
 $timeHash{'Day'} = $1;
 $timeHash{'Hour'} = $2;
 $timeHash{'Minute'} = $3;
 $timeHash{'Seconds'} = $4;
  print "Day = $timeHash{'Day'}; Hour = $timeHash{'Hour'}; " .
  "Minute = $timeHash{'Minute'}; Seconds = $timeHash{'Seconds'}\n";
}

This example only matches when *all* of the possibles are supplied, which is explicitly what he stated he didn't have.

There is probably a cool regex for this, but I am not a guru regexer, it might also be possible to do this with a double split, which while ugly, since the string is short probably wouldn't be terribly costly.

For instance, you could split once on \d to get the qualifiers, such as, 'h','m', then split again on \D to get the digits that are associated with each qualifier.... Assuming there is a 1 to 1 correspondence between digit groupings and a character.

http://danconia.org


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to