On Thu, Jun 11, 2009 at 10:51 AM, Michael Alipio <daem0n...@yahoo.com>wrote:

>
> Hi,
>
> I have a program that computes the number of elapsed minutes and seconds.
>
>
> if the outputs are:
>
> 2 minutes, and 8 seconds.
>
> How do I print those two values to look like "Elapsed time: 02:08"
>
> Thanks!
>
>
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>
What I would do (though there are many other ways of course)

$text = "2 minutes, and 8 seconds";

$text =~ /(\d{1,2}).*?(\d{1,2}).*/;

$text = sprintf("%02d:%02d", $1, $2);
print "Elapsed time: $text\n";

Using a regular expresion I take the first 1 or 2 digits, then anything in
the middle is ignored, and the next 1 or 2 digits are taken out. Then then
in text I using sprintf place the two values captured in the regex,
prepending a 0 in case it is only one digit putting a : in the middle.

Then all I do is print the thing and you are done.

Of course you can as I said do it in many different ways but this is the
simplest...

Reply via email to