List Info

Thread: Extracting numbers from a string




Extracting numbers from a string
user name
2006-03-22 14:00:33
This is a multipart message in MIME format.
--===============1897733010==
Content-Type: multipart/alternative;
	boundary="=_alternative 004CF62986257139_="

This is a multipart message in MIME format.
--=_alternative 004CF62986257139_=
Content-Type: text/plain; charset="us-ascii"

Erm, how about...

   $hr = $1;
   $min = $2;
   $sec = $3;

instead?  Just a thought.   





Ken Barker <kcbarkercenturytel.net>
Sent by: activeperl-bounceslistserv.ActiveState.com
03/21/2006 08:33 PM

 
        To:     "Naresh Bajaj"
<naresh.bajajgmail.com>, 
activeperllistserv.ActiveState.com
        cc: 
        Subject:        Re: Extracting numbers from a string


How about using a regex....

if($string =~  /PT(\d+)H(\d+)M(\d+).(\d+)/){
         $1=$hr;
         $2=$min;
         $3=$sec;
         $time = ($hr*3600)+($min*60)+$sec;
}

untested....


At 07:28 PM 3/21/2006, Naresh Bajaj wrote:
>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 Bajaj, Intern,
>Cardiac Rhythm Disease Management,
>Medtronic Inc.,
>763-514-3799
>
>_______________________________________________
>ActivePerl mailing list
>ActivePerllistserv.ActiveState.com
>To unsubscribe: http:/
/listserv.ActiveState.com/mailman/mysubs

_______________________________________________
ActivePerl mailing list
ActivePerllistserv.ActiveState.com
To unsubscribe: http:/
/listserv.ActiveState.com/mailman/mysubs



--=_alternative 004CF62986257139_=
Content-Type: text/html; charset="us-ascii"


<br><font size=2
face="sans-serif">Erm, how
about...</font>
<br>
<br><font size=2
face="sans-serif">&nbsp; &nbsp;$hr =
$1;</font>
<br><font size=2
face="sans-serif">&nbsp; &nbsp;$min =
$2;</font>
<br><font size=2
face="sans-serif">&nbsp; &nbsp;$sec =
$3;</font>
<br>
<br><font size=2
face="sans-serif">instead? &nbsp;Just a
thought. &nbsp; </font&g
t;
<br>
<br>
<br>
<br>
<table width=100%>
<tr valign=top>
<td>
<td><font size=1
face="sans-serif"><b>Ken Barker
&lt;kcbarkercenturytel.net&gt;</b></font>
<br><font size=1
face="sans-serif">Sent by:
activeperl-bounceslistserv.ActiveState.com</font>
<p><font size=1
face="sans-serif">03/21/2006 08:33
PM</font>
<br>
<td><font size=1
face="Arial">&nbsp; &nbsp; &nbsp;
&nbsp; </font>
<br><font size=1
face="sans-serif">&nbsp; &nbsp;
&nbsp; &nbsp; To: &nbsp; &nbsp; &nbsp;
&nbsp;&quot;Naresh Bajaj&quot;
&lt;naresh.bajajgmail.com&gt;, activeperllistserv.ActiveState.com</font>
<br><font size=1
face="sans-serif">&nbsp; &nbsp;
&nbsp; &nbsp; cc: &nbsp; &nbsp; &nbsp;
&nbsp;</font>
<br><font size=1
face="sans-serif">&nbsp; &nbsp;
&nbsp; &nbsp; Subject: &nbsp; &nbsp;
&nbsp; &nbsp;Re: Extracting numbers from a
string</font></table>
<br>
<br>
<br><font size=2 face="Courier
New">How about using a regex....<br>
<br>
if($string =~
&nbsp;/PT(\d+)H(\d+)M(\d+).(\d+)/){<br>
 &nbsp; &nbsp; &nbsp; &nbsp;
$1=$hr;<br>
 &nbsp; &nbsp; &nbsp; &nbsp;
$2=$min;<br>
 &nbsp; &nbsp; &nbsp; &nbsp;
$3=$sec;<br>
 &nbsp; &nbsp; &nbsp; &nbsp; $time =
($hr*3600)+($min*60)+$sec;<br>
}<br>
<br>
untested....<br>
<br>
<br>
At 07:28 PM 3/21/2006, Naresh Bajaj wrote:<br>
&gt;Hello all,<br>
&gt;I have one string PT2H46M7.815S.<br>
&gt;It is in hours, minutes and seconds. I want to
extract number between<br>
&gt;the alphabets and<br>
&gt;stored in seconds. How should I approach this
problem.<br>
&gt;Right now I am using the split command which is not
much elegant way.<br>
&gt;Crude method:<br>
&gt;extractinterval(PT7.815S)<br>
&gt;sub extractinterval{<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; my
$interval =$_[0];<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; my temp
=split //,$interval;<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; my
$output = $temp[2].$temp[3].$temp[4].$temp[5];<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp;
}<br>
&gt;Is there any other way of doing.<br>
&gt;Thanks,<br>
&gt;Naresh<br>
&gt;--<br>
&gt;Naresh Bajaj, Intern,<br>
&gt;Cardiac Rhythm Disease Management,<br>
&gt;Medtronic Inc.,<br>
&gt;763-514-3799<br>
&gt;<br>
&gt;_______________________________________________<b
r>
&gt;ActivePerl mailing list<br>
&gt;ActivePerllistserv.ActiveState.com<br>
&gt;To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs<br>

<br>
_______________________________________________<br>
ActivePerl mailing list<br>
ActivePerllistserv.ActiveState.com<br>
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs<br>

</font>
<br>
<br>
--=_alternative 004CF62986257139_=--


--===============1897733010==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
ActivePerl mailing list
ActivePerllistserv.ActiveState.com
To unsubscribe: http:/
/listserv.ActiveState.com/mailman/mysubs
--===============1897733010==--

[1]

about | contact  Other archives ( Real Estate discussion Medical topics )