[EMAIL PROTECTED] wrote on 03/21/2006 09:48:28 
PM:

> Today's Topics:
> 
>    7. Extracting numbers from a string (Naresh Bajaj)
> ----------------------------------------------------------------------
> ------------------------------
> 
> Message: 7
> Date: Tue, 21 Mar 2006 19:28:41 -0600
> From: "Naresh Bajaj" <[EMAIL PROTECTED]>
> Subject: Extracting numbers from a string
> To: [email protected]
> Message-ID:
>    <[EMAIL PROTECTED]>
> Content-Type: text/plain; charset=ISO-8859-1
> 
> Hello all,
> I have one string PT2H46M7.815S.
> It is in hours, minutes and seconds. I want to extract number between
> the alphabets and
> stored in seconds. How should I approach this problem.
> Right now I am using the split command which is not much elegant way.
> Crude method:
> extractinterval(PT7.815S)
> sub extractinterval{
>    my $interval =$_[0];
>    my @temp =split //,$interval;
>         my $output = $temp[2].$temp[3].$temp[4].$temp[5];
>    }
> Is there any other way of doing.
> Thanks,
> Naresh
> --
Naresh-

Are you trying to get the parts or total seconds?

--- untested code for parts ---
use strict;
use warnings;

foreach my $time (@ARGV){
  if ( $time =~ /pt(\d+)h(\d+)m([\d\.]+)/i ){
     print "hours: $1\tminutes: $2\tseconds $3\n";
  }
}
--- end untested code for parts ---
--- untested code for seconds ---
use strict;
use warnings;

foreach my $time (@ARGV){
  if ( $time =~ /pt(\d+)h(\d+)m([\d\.]+)/i ){

     my $seconds = $3;

     # add (((hours * 60 (min)) + minutes ) * 60 seconds) to seconds
     $seconds += ((($1 * 60) + $2) * 60);

     print "seconds: $seconds\n";
  }
}
--- end untested code for seconds ---

HTH

-Josh

-----------------------------------------
PLEASE NOTE: 
SeaChange International headquarters in Maynard, MA is moving!
Effective March 1, 2006, our new headquarters address will be:

SeaChange International 
50 Nagog Park 
Acton, MA 01720 USA 

All telephone numbers remain the same: 
Main Corporate Telephone: 978-897-0100 
Customer Service Telephone: 978-897-7300

_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to