AutoHotkey Community

It is currently May 27th, 2012, 2:13 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 11 posts ] 
Author Message
 Post subject: forcing a calculation
PostPosted: February 3rd, 2010, 11:36 am 
Offline

Joined: February 7th, 2008, 9:48 pm
Posts: 509
Code:
a = 5
msgbox  % a+2


gives you 7

But i am stuck here:

How to force the calculation here ?The var a is a result of regex so its all characters.

Code:
a = 5+2
msgbox  % a


thanks


Report this post
Top
 Profile  
Reply with quote  
PostPosted: February 3rd, 2010, 11:39 am 
Offline

Joined: August 13th, 2006, 6:45 am
Posts: 355
Location: Germany
Code:
a := 5+2
msgbox  % a


Hubert


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 3rd, 2010, 11:44 am 
Offline
User avatar

Joined: September 5th, 2009, 2:06 pm
Posts: 1713
Location: Somewhere near you
SOLVED

_________________
Image
The quick onyx goblin jumps over the lazy dwarf


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 3rd, 2010, 12:03 pm 
Offline

Joined: February 7th, 2008, 9:48 pm
Posts: 509
What do you mean solved?

I know that a:=5+2 is 7 but that is not what i asked!!!


example

Code:
data=aa 5 ggg 6

a:=RegExreplace( data,".* (\d+).*g (\d+)","$1+$2" )

MsgBox, %a%


Maybe i should have given an example in my first post :cry:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 3rd, 2010, 1:38 pm 
yume wrote:
What do you mean solved?

I know that a:=5+2 is 7 but that is not what i asked!!!


example

Code:
data=aa 5 ggg 6

a:=RegExreplace( data,".* (\d+).*g (\d+)","$1+$2" )

MsgBox, %a%


Maybe i should have given an example in my first post :cry:

Maybe you should carefully read the help page for the RegexReplace "replacement" argument".
Quote:
Replacement
The string to be substituted for each match, which is plain text...

This is probably the best you will get:
Code:
data=aa 5 ggg 6

RegExMatch( data,".* (\d+).*g (\d+)",out )
a := out1 + out2
msgbox, %a%

MsgBox, %a%

What you suggested maybe possible with "regex callouts", which ahk does not provide, but even if it did, you would still need an external function to to the maths.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: February 3rd, 2010, 2:00 pm 
Offline

Joined: February 7th, 2008, 9:48 pm
Posts: 509
thanks wanse

but it is not only regex that is why i gave the first example ( maybe misleading)

data=aa5a+a6
Stringreplace,a,data,a,,all


a = 5+2

If you want to force it to calculate the "string" "5 + 2" contained in var a there is no way to do it?

You can get this same string from other string manipulations than regex but in the end you are stuck if you want to ahk see it as an expression?

something like result :=a + 0 ( tried but does not work )

Unless the workaround you propose.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 3rd, 2010, 2:17 pm 
yume wrote:
thanks wanse
If you want to force it to calculate the "string" "5 + 2" contained in var a there is no way to do it?

Ok, these two links demonstrate how to evaluate expressions in strings.
Monster: evaluate math expressions in strings
Popup calculator / expression evaluator
If you search the forums, I think there are a couple of other demonstrations of how to do it.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: February 3rd, 2010, 2:22 pm 
oops, got the links mixed :oops:
Monster: evaluate math expressions in strings
Popup calculator / expression evaluator


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: February 3rd, 2010, 4:41 pm 
Offline
User avatar

Joined: March 19th, 2008, 12:43 am
Posts: 5480
Location: the tunnel(?=light)
wanse wrote:
This is probably the best you will get...


A while loop will get it:

Code:
data=aa 55 ggg 6 bb 711 hhh 18 cc -1 iii 2
Pos=1
While Pos:=RegExMatch(data,"-?\d+",m,Pos+StrLen(m))
   res+=m

MsgBox % res


wanse wrote:
What you suggested maybe possible with "regex callouts", which ahk does not provide...


AHK does not but AutoHotkey_L does. I don't think callouts will necessarily help here since you must know the number of matches you're going to have ahead of time.

_________________
Image
Try Quick Search for Autohotkey or see the tutorial for newbies.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 3rd, 2010, 5:09 pm 
sinkfaze wrote:
wanse wrote:
What you suggested maybe possible with "regex callouts", which ahk does not provide...


AHK does not but AutoHotkey_L does. I don't think callouts will necessarily help here since you must know the number of matches you're going to have ahead of time.


Well, the rest of my statememnt was
Quote:
...but even if it did, you would still need an external function to to the maths.
And that is still true with AHK_L.

And there is a drawback to AHK_L (besides learning a new set of idiosyncrasies/bugs) and that is that Win9x compatibility is lost. Maybe it is not an issue for you, but for me and a few others, it is a concern


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: February 3rd, 2010, 5:41 pm 
Offline

Joined: February 7th, 2008, 9:48 pm
Posts: 509
thank you all :)


to hd0202 sorry i should have made my question clearer ( i had been going over the problem for 2 hours..........)

to sinkfaze looks like the best solution i can get,when i asked the question i thought it would be some trick like the forcing of expression by % var

to wanze thanks i looked at the calculator it solves the problem using a coded function but your first regexmatch solution was an easier way to do it for that case.(maybe not an universal solution )


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], Google Feedfetcher, nimda, poserpro, rbrtryn, sjc1000, Yahoo [Bot] and 15 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group