Page 1 of 1

Converting numeric substrings to numbers for calculations

Posted: Fri Oct 16, 2009 4:47 pm
by barry.marcus
I have a need to search for numeric portions of strings in my data, do a simple calculation on them, and reinsert the result back into the string forming a new string. I can't seem to get my head around how I would do it.

Specifically, supposing I had a string that looked like this:

<$inData="action/4 request/3 dimension/2">

How would I, say, double the "request" identifier and so that the resulting string is:

"action/4 request/6 dimension/2"

I don't necessarily want to always double values, but I'm just saying this as an example. Once I figure out how to extract the specific numeric piece and reinsert it back into the string, I can handle all the various calcs I need to do.

Thanks in advance

Converting numeric substrings to numbers for calculations

Posted: Fri Oct 16, 2009 5:51 pm
by mark
Use sandcall
http://www.thunderstone.com/site/vortexman/fmtcp.html
http://www.thunderstone.com/texis/site/ ... dcall.html

<a name=numbers hit>
<$ret=(2*$hit)>$ret<!--nonewline-->
</a>
<a name=main>
<$inData="action/4 request/3 dimension/2">
<fmtcp sandcall "\digit+" numbers>
In: $inData
Out: <sb>$inData</sb>

</a>

Converting numeric substrings to numbers for calculations

Posted: Fri Oct 16, 2009 5:52 pm
by mark
use <capture><sb>$inData</sb></capture><$outData=$ret> to put into a string instead of print.

Converting numeric substrings to numbers for calculations

Posted: Sat Oct 17, 2009 4:19 pm
by barry.marcus
Thanks! This works great!