Converting Text to Numeric

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
evangelmike
Pro Scripter
Posts: 56
Joined: Sun May 11, 2008 9:39 pm

Converting Text to Numeric

Post by evangelmike » Fri Oct 30, 2009 6:19 am

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.
May you have a blessed day!

Michael D Fitzpatrick
Reg. US Patent Agent

User avatar
Marcus Tettmar
Site Admin
Posts: 7348
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Fri Oct 30, 2009 9:09 am

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.:

Code: Select all

VBSTART
VBEND

Let>text= 1.01  
VBEval>Trim("%text%"),num
Let>num=num+1
MessageModal>num

Note that
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.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

evangelmike
Pro Scripter
Posts: 56
Joined: Sun May 11, 2008 9:39 pm

Thanks greatly! But still a slight problem

Post by evangelmike » Fri Oct 30, 2009 4:34 pm

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
May you have a blessed day!

Michael D Fitzpatrick
Reg. US Patent Agent

User avatar
Marcus Tettmar
Site Admin
Posts: 7348
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Fri Oct 30, 2009 5:08 pm

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:

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
Use it like this:

Code: Select all

Let>myStr= 4.5 *
GoSub>CleanString,myStr
MessageModal>CleanString_Result
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

Code: Select all

Let>valid_chars=0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
Or any other series of characters you want.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
Marcus Tettmar
Site Admin
Posts: 7348
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Fri Oct 30, 2009 5:16 pm

Nearly forgot about RegEx!

RegEx to remove all non numerics:

Code: Select all

RegEx>[^.0123456789],myStr,0,matches,num,1,,myStr
RegEx to remove all control chars:

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?

evangelmike
Pro Scripter
Posts: 56
Joined: Sun May 11, 2008 9:39 pm

Necessity of 2 StringReplaces before use CleanString

Post by evangelmike » Sun Nov 01, 2009 4:09 am

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%
May you have a blessed day!

Michael D Fitzpatrick
Reg. US Patent Agent

User avatar
jpuziano
Automation Wizard
Posts: 1086
Joined: Sat Oct 30, 2004 12:00 am

Post by jpuziano » Thu Jan 21, 2010 8:05 am

mtettmar wrote:RegEx to remove all control chars:

Code: Select all

RegEx>[\x00-\x1f],myStr,0,matches,num,1,,myStr
Hi Marcus,

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.
EasyPatterns way to strip all control chars including ascii 127 (delete) wrote:RegEx>[controlChar],myStr,1,matches,num,1,,myStr
And if you didn't want to strip 127 (delete), you could use [gremlin].
EasyPatterns 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.
Take care
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 - :-)

Neib74656
Junior Coder
Posts: 27
Joined: Fri Sep 10, 2021 10:51 pm

Re: Converting Text to Numeric

Post by Neib74656 » Tue Jul 12, 2022 5:46 am

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

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
The first messagemodal returns 2 but so does the second one when it should return 3 if I am understanding correctly?

Thanks as always!

Neib74656
Junior Coder
Posts: 27
Joined: Fri Sep 10, 2021 10:51 pm

Re: Converting Text to Numeric

Post by Neib74656 » Tue Jul 12, 2022 6:22 am

Neib74656 wrote:
Tue Jul 12, 2022 5:46 am
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

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
The first messagemodal returns 2 but so does the second one when it should return 3 if I am understanding correctly?

Thanks as always!
Realized my error. It actually is working as intended but my let syntax is wrong. it should be let>W=W+1

Please ignore me and ........... maybe I will go away :P

hagchr
Automation Wizard
Posts: 314
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: Converting Text to Numeric

Post by hagchr » Tue Jul 12, 2022 6:45 am

Small tip. If you are just looking for the digit/number you can search for that directly eg:

Code: Select all

regex>\d,myStr,0,matches,num,0
\d will look for a digit, and your number will be in matches_1.

Neib74656
Junior Coder
Posts: 27
Joined: Fri Sep 10, 2021 10:51 pm

Re: Converting Text to Numeric

Post by Neib74656 » Wed Jul 13, 2022 11:05 pm

hagchr wrote:
Tue Jul 12, 2022 6:45 am
Small tip. If you are just looking for the digit/number you can search for that directly eg:

Code: Select all

regex>\d,myStr,0,matches,num,0
\d will look for a digit, and your number will be in matches_1.
Does it look for only a single digit? I used this as an example but the number can be anywhere from 1 - 99999.

hagchr
Automation Wizard
Posts: 314
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: Converting Text to Numeric

Post by hagchr » Thu Jul 14, 2022 6:18 am

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.

Neib74656
Junior Coder
Posts: 27
Joined: Fri Sep 10, 2021 10:51 pm

Re: Converting Text to Numeric

Post by Neib74656 » Sun Jul 17, 2022 7:06 am

hagchr wrote:
Thu Jul 14, 2022 6:18 am
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.
No worries works perfectly thanks very much!

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts