Jump to content
Sign in to follow this  
jorgeng

Replace string

Recommended Posts

jorgeng

I have a string which looks like this:

15:47 ORDER "sl) Omx Sarlacc - Sälj Trade - Rosa - Krypterat OMXS300G" kurs 1012.75

I want to subtract 0.50 to 1012.75 giving me 1012.25.

Is this possible to do in a calculation string?

:mellow:

Above is just an example, the numbers can be others and change often.

Edited by jorgeng

Share this post


Link to post
Share on other sites
PsaltyDS

Extract that part of the string with StringMid() or other string manipulation, or maybe StringRegExp(). Do the math, then put the result back in the string.

If you get stuck, post what you tried for more help.

:mellow:


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
Paulie

$string='15:47 ORDER "sl) Omx Sarlacc - Sälj Trade - Rosa - Krypterat OMXS300G" kurs 1012.75'
$result = stringregexp($string, "\d*\.\d{2}",3)
If isarray($result) then
$result = $result[0]-.5
Msgbox(0,"",$result)
Endif

Share this post


Link to post
Share on other sites
GEOSoft

And another simple way

$sStr = '15:47 ORDER "sl) Omx Sarlacc - Sälj Trade - Rosa - Krypterat OMXS300G" kurs 1012.75'

$iNewValue = StringRegExpReplace($sStr, "^.+\h([\d.]+)$", "$1") -.5
If @Extended Then
    MsgBox(0, "Result", $iNewValue)
Else
    MsgBox(0, "Error", "Invalid string entered")
EndIf

Edit: Added code to check for a valid string.

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites
jorgeng

Thanks everybody.

They all works, but i want to have the result replaced in original string, how to do that?

Now the string looks like:

15:47 ORDER "sl) Omx Sarlacc - Sälj Trade - Rosa - Krypterat OMXS300G" kurs 1012.75

and i want

15:47 ORDER "sl) Omx Sarlacc - Sälj Trade - Rosa - Krypterat OMXS300G" kurs 1012.25

How to do that?

:mellow:

Share this post


Link to post
Share on other sites
JohnOne

$string='15:47 ORDER "sl) Omx Sarlacc - Sälj Trade - Rosa - Krypterat OMXS300G" kurs 1012.75'
$a=StringRegExp($string,"kurs (\S*)",1)
$b = $a[0] - 0.5
ConsoleWrite($b & @LF)
$c = StringReplace($string,$a[0],$b)
ConsoleWrite($c & @LF)


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites
jorgeng

One small problem, when price is 1012.50 and script will subtract 0.50 the

result becomes 1012

and i want 1012.00

How to fix this?

:mellow:

Share this post


Link to post
Share on other sites
GEOSoft

$sStr = '15:47 ORDER "sl) Omx Sarlacc - Sälj Trade - Rosa - Krypterat OMXS300G" kurs 1012.50'
$aStr = StringRegExp($sStr, "^(.+\h)([\d.]+)$", 1)
If IsArray($aStr) Then
    $aStr[1] = StringFormat( "%.2f", $aStr[1] -.5)
    $sStr = $aStr[0] & $aStr[1]
    MsgBox(0, "Result", $sStr)
Else
    MsgBox(0, "Error", "Invalid string entered")
EndIf


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites
JohnOne

Or

$string='15:47 ORDER "sl) Omx Sarlacc - Sälj Trade - Rosa - Krypterat OMXS300G" kurs 1012.75'
$a=StringRegExp($string,"kurs (\S*)",1)
$b = $a[0] - .5
If Not StringInStr($b,".") Then $b &= ".00"
$c = StringReplace($string,$a[0],$b)
ConsoleWrite($c & @LF)

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites
jorgeng

Thanks.

Works fine.

:mellow:

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×