Converting Text to Numeric
Moderators: Dorian (MJT support), JRL
-
- Pro Scripter
- Posts: 56
- Joined: Sun May 11, 2008 9:39 pm
Converting Text to Numeric
Greetings! I am writing a macro to pick the stock price off a web page. The following instructions indeed pick the stock price, but the result is text instead of numeric. My instructions are:
GetTextInRect>162,404,198,423,sPriceCRLF
StringReplace>sPriceCRLF, ,,PriceCRLF
StringReplace>PriceCRLF,CRLF,,Price
If I use MessageModal to display the price, the result would be text such as 1.10. But if I then add 1.0 and display the result, I get a display of 1.10+1.0 instead of 2.10. Has anyone written a routine to convert text to numeric. I didn't want to reinvent the wheel. Thanks for any comments, code, or suggestions.
GetTextInRect>162,404,198,423,sPriceCRLF
StringReplace>sPriceCRLF, ,,PriceCRLF
StringReplace>PriceCRLF,CRLF,,Price
If I use MessageModal to display the price, the result would be text such as 1.10. But if I then add 1.0 and display the result, I get a display of 1.10+1.0 instead of 2.10. Has anyone written a routine to convert text to numeric. I didn't want to reinvent the wheel. Thanks for any comments, code, or suggestions.
May you have a blessed day!
Michael D Fitzpatrick
Reg. US Patent Agent
Michael D Fitzpatrick
Reg. US Patent Agent
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
Macro Scheduler automatically converts from text to numeric when you do a calculation like that. So the fact that isn't happening for you tells me there must be a space or some other non-numeric character in the string. You might just need to trim it. Eg.:
Note the space in front of 1.01 which means that text is text, not numeric. After trimming it we add 1 to it and we get 2.01 proving that the conversion is automatic so long as the data contains only numbers.
Code: Select all
VBSTART
VBEND
Let>text= 1.01
VBEval>Trim("%text%"),num
Let>num=num+1
MessageModal>num
Note that
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
-
- Pro Scripter
- Posts: 56
- Joined: Sun May 11, 2008 9:39 pm
Thanks greatly! But still a slight problem
Greetings! Thanks immensely! I modified my code as follows. It works great when "right"=193. But if "right" is 194, there is an extra nonprintable character at the end of the string sPriceCRLF, which none of the below logic will remove. I really need to use 194 so I can accomodate larger stock prices. All suggestions, comments, code greatly welcomed.
GetTextInRect>162,385,193,406,sPriceCRLF
// NOTE: when I use right=194 (instead of 193), a nonprintable char
// appears at end of string which TRIM won't remove.
StringReplace>sPriceCRLF, ,,PriceCRLF
StringReplace>PriceCRLF, ,,PriceCRLF
StringReplace>PriceCRLF,CRLF,,Price
VBSTART
VBEND
VBEval>Trim("%Price%"),num
MessageModal>num
Let>num=num+1
MessageModal>num
GetTextInRect>162,385,193,406,sPriceCRLF
// NOTE: when I use right=194 (instead of 193), a nonprintable char
// appears at end of string which TRIM won't remove.
StringReplace>sPriceCRLF, ,,PriceCRLF
StringReplace>PriceCRLF, ,,PriceCRLF
StringReplace>PriceCRLF,CRLF,,Price
VBSTART
VBEND
VBEval>Trim("%Price%"),num
MessageModal>num
Let>num=num+1
MessageModal>num
May you have a blessed day!
Michael D Fitzpatrick
Reg. US Patent Agent
Michael D Fitzpatrick
Reg. US Patent Agent
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
Ok, like you say, some non-space non-printable char in there somewhere. Here's a subroutine which will remove all characters except the ones you want:
Use it like this:
Pass the clipboard value to the subroutine and it should come back clean!
This SRT could be converted for alphanumerics by changing valid_chars to
Or any other series of characters you want.
Code: Select all
SRT>CleanString
Let>CleanString_Result=
Let>valid_chars=.0123456789
Length>CleanString_Var_1,len
Let>k=0
Repeat>k
Let>k=k+1
MidStr>myStr,k,1,this_char
Position>this_char,valid_chars,1,p
If>p>0
Let>CleanString_Result=%CleanString_Result%%this_char%
Endif
Until>k=len
End>CleanString
Code: Select all
Let>myStr= 4.5 *
GoSub>CleanString,myStr
MessageModal>CleanString_Result
This SRT could be converted for alphanumerics by changing valid_chars to
Code: Select all
Let>valid_chars=0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
Nearly forgot about RegEx!
RegEx to remove all non numerics:
RegEx to remove all control chars:
RegEx to remove all non numerics:
Code: Select all
RegEx>[^.0123456789],myStr,0,matches,num,1,,myStr
Code: Select all
RegEx>[\x00-\x1f],myStr,0,matches,num,1,,myStr
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
-
- Pro Scripter
- Posts: 56
- Joined: Sun May 11, 2008 9:39 pm
Necessity of 2 StringReplaces before use CleanString
Hi Marcus! Thanks for your kind help.
It is interesting to note that if I omit either a StringReplace to remove blanks or a StringReplace to remove to remove the appended CRLF, I get the message "Error in Position Command" when I run the below instructions. Why would that be? If I include the StringReplaces, the code runs without error.
GetTextInRect>162,403,192,424,sPriceCRLF
StringReplace>sPriceCRLF, ,,PriceCRLF
StringReplace>PriceCRLF,CRLF,,Price
MessageModal>%Price%
GoSub>CleanString,Price
// Next instruction to verify that result is indeed numeric:
Let>Price=Price+1.5
MessageModal>%Price%
It is interesting to note that if I omit either a StringReplace to remove blanks or a StringReplace to remove to remove the appended CRLF, I get the message "Error in Position Command" when I run the below instructions. Why would that be? If I include the StringReplaces, the code runs without error.
GetTextInRect>162,403,192,424,sPriceCRLF
StringReplace>sPriceCRLF, ,,PriceCRLF
StringReplace>PriceCRLF,CRLF,,Price
MessageModal>%Price%
GoSub>CleanString,Price
// Next instruction to verify that result is indeed numeric:
Let>Price=Price+1.5
MessageModal>%Price%
May you have a blessed day!
Michael D Fitzpatrick
Reg. US Patent Agent
Michael D Fitzpatrick
Reg. US Patent Agent
Hi Marcus,mtettmar wrote:RegEx to remove all control chars:
Code: Select all
RegEx>[\x00-\x1f],myStr,0,matches,num,1,,myStr
Wikipedia includes ascii 127 (delete) as a control character: http://en.wikipedia.org/wiki/Control_character
I'm not sure if the delete character would cause problems if passed to a VBScript function but I strip them out as well to be safe.
And if you didn't want to strip 127 (delete), you could use [gremlin].EasyPatterns way to strip all control chars including ascii 127 (delete) wrote:RegEx>[controlChar],myStr,1,matches,num,1,,myStr
Take careEasyPatterns Reference wrote:[controlChar]
characters 0-31, 127 (careful: includes most whitespace)
[gremlin]
characters 0-31. The definition for [gremlin] is more cautious than in some products.
jpuziano
Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post -
Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post -
Re: Converting Text to Numeric
Reviving a very old thread here.
Hello Everyone,
I have a need for something similar to this but I can't seem to get it to work. I am using OCRarea to capture text and symbols on my screen. The OCRarea will give 1 of two possible results.
1)string - 2 @
2)string - 2 ©
I want to isolate the numeric value and then use it in a calculation here is my current code
The first messagemodal returns 2 but so does the second one when it should return 3 if I am understanding correctly?
Thanks as always!
Hello Everyone,
I have a need for something similar to this but I can't seem to get it to work. I am using OCRarea to capture text and symbols on my screen. The OCRarea will give 1 of two possible results.
1)string - 2 @
2)string - 2 ©
I want to isolate the numeric value and then use it in a calculation here is my current code
Code: Select all
OCRArea>C,D,E,F,myStr
regex>[^.0123456789],myStr,0,matches,num,1,,myStr
RegEx>[\x00-\x1f],myStr,0,matches,num,1,,W
messagemodal>W
let>W+1=W
messagemodal>W
Thanks as always!
Re: Converting Text to Numeric
Realized my error. It actually is working as intended but my let syntax is wrong. it should be let>W=W+1Neib74656 wrote: ↑Tue Jul 12, 2022 5:46 amReviving a very old thread here.
Hello Everyone,
I have a need for something similar to this but I can't seem to get it to work. I am using OCRarea to capture text and symbols on my screen. The OCRarea will give 1 of two possible results.
1)string - 2 @
2)string - 2 ©
I want to isolate the numeric value and then use it in a calculation here is my current code
The first messagemodal returns 2 but so does the second one when it should return 3 if I am understanding correctly?Code: Select all
OCRArea>C,D,E,F,myStr regex>[^.0123456789],myStr,0,matches,num,1,,myStr RegEx>[\x00-\x1f],myStr,0,matches,num,1,,W messagemodal>W let>W+1=W messagemodal>W
Thanks as always!
Please ignore me and ........... maybe I will go away
Re: Converting Text to Numeric
Small tip. If you are just looking for the digit/number you can search for that directly eg:
\d will look for a digit, and your number will be in matches_1.
Code: Select all
regex>\d,myStr,0,matches,num,0
Re: Converting Text to Numeric
Does it look for only a single digit? I used this as an example but the number can be anywhere from 1 - 99999.hagchr wrote: ↑Tue Jul 12, 2022 6:45 amSmall tip. If you are just looking for the digit/number you can search for that directly eg:
\d will look for a digit, and your number will be in matches_1.Code: Select all
regex>\d,myStr,0,matches,num,0
Re: Converting Text to Numeric
Sorry, I thought you were just looking for one digit. Simply change from \d (one digit) to \d+ (one or more digits) and you should catch all.