Re: 2 minutes and 8 second (how do i convert it to 02:08)??

2009-06-11 Thread Gunnar Hjalmarsson

Rob Coops wrote:


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

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

-^^
When you are extracting stuff, .* is _always_ redundant in the beginning 
or end of a regex.


--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: 2 minutes and 8 second (how do i convert it to 02:08)??

2009-06-11 Thread Jim Gibson
On 6/11/09 Thu  Jun 11, 2009  1:51 AM, "Michael Alipio"
 scribbled:

> 
> 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"

This is how I would do it (untested):

my $text = '2 minutes, and 8 seconds';

if( $text =~ m{ (\d+) \s* minutes .* and \s* (\d+) \s* seconds }x ) {
printf "Elapsed time: %02d:%02d\n";
}else{
print "Invalid format: $text\n", $1, $2;
}


Note that it is important to test whether the regular expression matches
before using the capture variables $1 and $2.




-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: 2 minutes and 8 second (how do i convert it to 02:08)??

2009-06-11 Thread Rob Coops
On Thu, Jun 11, 2009 at 10:51 AM, Michael Alipio 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...


2 minutes and 8 second (how do i convert it to 02:08)??

2009-06-11 Thread Michael Alipio

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/