| View previous topic :: View next topic |
| Author |
Message |
closed
Joined: 07 Feb 2008 Posts: 509
|
Posted: Wed Feb 03, 2010 10:36 am Post subject: forcing a calculation |
|
|
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.
thanks |
|
| Back to top |
|
 |
hd0202
Joined: 13 Aug 2006 Posts: 265 Location: Germany
|
Posted: Wed Feb 03, 2010 10:39 am Post subject: Re: forcing a calculation |
|
|
| Code: | a := 5+2
msgbox % a |
Hubert |
|
| Back to top |
|
 |
tomoe_uehara
Joined: 05 Sep 2009 Posts: 1591 Location: Somewhere near you
|
|
| Back to top |
|
 |
closed
Joined: 07 Feb 2008 Posts: 509
|
Posted: Wed Feb 03, 2010 11:03 am Post subject: |
|
|
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  |
|
| Back to top |
|
 |
wanse Guest
|
Posted: Wed Feb 03, 2010 12:38 pm Post subject: |
|
|
| 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  |
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
|
Posted: Wed Feb 03, 2010 1:00 pm Post subject: |
|
|
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 |
|
 |
wanse Guest
|
Posted: Wed Feb 03, 2010 1:17 pm Post subject: |
|
|
| 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
|
|
| Back to top |
|
 |
sinkfaze
Joined: 18 Mar 2008 Posts: 5044 Location: the tunnel(?=light)
|
Posted: Wed Feb 03, 2010 3:41 pm Post subject: |
|
|
| 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 |
|
 |
wanse Guest
|
Posted: Wed Feb 03, 2010 4:09 pm Post subject: |
|
|
| 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
|
Posted: Wed Feb 03, 2010 4:41 pm Post subject: |
|
|
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 ) |
|
| Back to top |
|
 |
|