AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

forcing a calculation

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
closed



Joined: 07 Feb 2008
Posts: 509

PostPosted: Wed Feb 03, 2010 10:36 am    Post subject: forcing a calculation Reply with quote

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
Back to top
View user's profile Send private message
hd0202



Joined: 13 Aug 2006
Posts: 265
Location: Germany

PostPosted: Wed Feb 03, 2010 10:39 am    Post subject: Re: forcing a calculation Reply with quote

Code:
a := 5+2
msgbox  % a


Hubert
Back to top
View user's profile Send private message
tomoe_uehara



Joined: 05 Sep 2009
Posts: 1591
Location: Somewhere near you

PostPosted: Wed Feb 03, 2010 10:44 am    Post subject: Reply with quote

SOLVED
_________________

The quick onyx goblin jumps over the lazy dwarf
Back to top
View user's profile Send private message
closed



Joined: 07 Feb 2008
Posts: 509

PostPosted: Wed Feb 03, 2010 11:03 am    Post subject: Reply with quote

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 Crying or Very sad
Back to top
View user's profile Send private message
wanse
Guest





PostPosted: Wed Feb 03, 2010 12:38 pm    Post subject: Reply with quote

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 Crying or Very sad

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.
Back to top
closed



Joined: 07 Feb 2008
Posts: 509

PostPosted: Wed Feb 03, 2010 1:00 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
wanse
Guest





PostPosted: Wed Feb 03, 2010 1:17 pm    Post subject: Reply with quote

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.
Back to top
wanse
Guest





PostPosted: Wed Feb 03, 2010 1:22 pm    Post subject: Reply with quote

oops, got the links mixed Embarassed
Monster: evaluate math expressions in strings
Popup calculator / expression evaluator
Back to top
sinkfaze



Joined: 18 Mar 2008
Posts: 5044
Location: the tunnel(?=light)

PostPosted: Wed Feb 03, 2010 3:41 pm    Post subject: Reply with quote

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.
_________________
Try Quick Search for Autohotkey or see the tutorial for newbies.
Back to top
View user's profile Send private message Send e-mail
wanse
Guest





PostPosted: Wed Feb 03, 2010 4:09 pm    Post subject: Reply with quote

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
Back to top
closed



Joined: 07 Feb 2008
Posts: 509

PostPosted: Wed Feb 03, 2010 4:41 pm    Post subject: Reply with quote

thank you all Smile


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 )
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group