| View previous topic :: View next topic |
| Author |
Message |
jethrow
Joined: 24 May 2009 Posts: 1907 Location: Iowa, USA
|
Posted: Sun Mar 14, 2010 5:21 am Post subject: Distinguish between a String & Number |
|
|
Is there a way to distinguish between say 123 and "123" in AHK? I know I've seen these treated differently when working with COM. Is there perhaps a simple dll call that could determine the difference? _________________
- in case I forgot to smile
Basic Webpage Controls
COM Object Reference |
|
| Back to top |
|
 |
Murx Guest
|
Posted: Sun Mar 14, 2010 6:00 am Post subject: |
|
|
| Code: | MsgBox % 123/1
MsgBox % "123"/1 | ? |
|
| Back to top |
|
 |
jethrow
Joined: 24 May 2009 Posts: 1907 Location: Iowa, USA
|
Posted: Sun Mar 14, 2010 6:44 am Post subject: |
|
|
Thanks for the reply Murx. I should clarify more - I want to pass the contents to a function. So, here's where the issue becomes relevant: | Code: | var := 123
var2 := "123"
MsgBox % var/1
MsgBox % var2/1
If var is integer
MsgBox
If var2 is integer
MsgBox |
_________________
- in case I forgot to smile
Basic Webpage Controls
COM Object Reference |
|
| Back to top |
|
 |
Murx Guest
|
Posted: Sun Mar 14, 2010 7:45 am Post subject: |
|
|
| Code: | var = 123
var2 = "123"
MyFunc(var)
MyFunc(var2)
MyFunc(var) {
If va is integer
MsgBox % var
Return
} | ? |
|
| Back to top |
|
 |
Murx Guest
|
Posted: Sun Mar 14, 2010 7:46 am Post subject: |
|
|
Ouch!
| Code: | [...]
If var is integer
[...] |
|
|
| Back to top |
|
 |
OceanMachine
Joined: 15 Oct 2007 Posts: 780 Location: England
|
Posted: Sun Mar 14, 2010 7:56 am Post subject: |
|
|
| Murx wrote: | | Code: | var = 123
var2 = "123"
MyFunc(var)
MyFunc(var2)
MyFunc(var) {
If var is integer
MsgBox % var
Return
} | ? |
That won't work, because one of the variables you are assigning contains a 'literal' pair of quotes and the other doesn't.
If you use the ":=" assignment operator instead, this is like saying | Code: | var1 := 123
var2 := """123"""
... |
..which will mean they are not the same |
|
| Back to top |
|
 |
Sean
Joined: 12 Feb 2007 Posts: 2462
|
Posted: Sun Mar 14, 2010 11:42 am Post subject: |
|
|
| AHK doesn't expose any way for a script to distinguish the two. |
|
| Back to top |
|
 |
Tuncay
Joined: 07 Nov 2006 Posts: 1886 Location: Germany
|
Posted: Sun Mar 14, 2010 11:57 am Post subject: |
|
|
At the time of assignment, the type of variable is converted to string allways. It is dynamically converted back to number, if needed. In example at math operations.
That is what Sean said, no. _________________ {1:"ahkstdlib", 2:"my libs", 3:"my apps", 4:"my license"}
--> Don't feed the troll! <-- |
|
| Back to top |
|
 |
jaco0646
Joined: 07 Oct 2006 Posts: 3113 Location: MN, USA
|
Posted: Sun Mar 14, 2010 4:25 pm Post subject: |
|
|
| OceanMachine wrote: | | ..which will mean they are not the same | That's the point.
What's wrong with the example by Murx? It distinguishes between 123 and "123" just fine (after the spelling correction). | Code: | var = 123 ;Int
var2 = "123" ;Str
MsgBox,% MyFunc(var)
MsgBox,% MyFunc(var2)
ExitApp
MyFunc(var) {
If var is integer
Return, "Int"
Else Return, "Str"
} |
Last edited by jaco0646 on Sun Mar 14, 2010 4:29 pm; edited 1 time in total |
|
| Back to top |
|
 |
Tuncay
Joined: 07 Nov 2006 Posts: 1886 Location: Germany
|
Posted: Sun Mar 14, 2010 4:28 pm Post subject: |
|
|
Yes that would work. but this is a way where the user make extra effort to save the type by himself and manages. My point is about that AutoHotkey drops the type of variable. The example of Murx is a way how the user manages on its own to remember which variable have which type. _________________ {1:"ahkstdlib", 2:"my libs", 3:"my apps", 4:"my license"}
--> Don't feed the troll! <-- |
|
| Back to top |
|
 |
jaco0646
Joined: 07 Oct 2006 Posts: 3113 Location: MN, USA
|
Posted: Sun Mar 14, 2010 4:33 pm Post subject: |
|
|
| Tuncay wrote: | | the user make extra effort | jethrow was already doing that (just using the wrong operator). |
|
| Back to top |
|
 |
Tuncay
Joined: 07 Nov 2006 Posts: 1886 Location: Germany
|
Posted: Sun Mar 14, 2010 4:38 pm Post subject: |
|
|
I did not realize that he WANT write the surrounding quotes for distinguishing. Then your example is working like jethrow should want to. here is another possible way.
| Code: | var = 123 ;Int
var2 = "123" ;Str
MsgBox,% isString(var)
MsgBox,% isString(var2)
isString(ByRef _var)
{
Return (SubStr(_var, 1, 1) = """")
} |
_________________ {1:"ahkstdlib", 2:"my libs", 3:"my apps", 4:"my license"}
--> Don't feed the troll! <-- |
|
| Back to top |
|
 |
Sean
Joined: 12 Feb 2007 Posts: 2462
|
Posted: Sun Mar 14, 2010 4:42 pm Post subject: |
|
|
The target data is 123, not "123". Sometimes you need to pass it as an integer, sometimes as a string. Some languages distinguish the two as:
| Code: | x := 123 ; integer
x := "123" ; string
|
|
|
| Back to top |
|
 |
HotKeyIt
Joined: 18 Jun 2008 Posts: 4652 Location: AHK Forum
|
Posted: Sun Mar 14, 2010 5:18 pm Post subject: |
|
|
There is a way to distinguish  | Code: | var := 123 ;Int
var2 := "123" ;Str
MsgBox,% MyFunc(var)
MsgBox,% MyFunc(var2)
ExitApp
MyFunc(var=0) {
Format:=A_FormatInteger
SetFormat,IntegerFast,Hex
variable:=var ;will not result to hex if it is not an integer
If (SubStr(variable,1,2)="0x")
return:="Int"
else return:="Str"
SetFormat,IntegerFast,%Format%
Return return
} |
_________________ AHK_H (2alpha) AHF TT _Struct WatchDir Yaml _Input ObjTree RapidHotkey DynaRun 
Last edited by HotKeyIt on Mon Mar 15, 2010 1:30 pm; edited 2 times in total |
|
| Back to top |
|
 |
jethrow
Joined: 24 May 2009 Posts: 1907 Location: Iowa, USA
|
Posted: Sun Mar 14, 2010 6:19 pm Post subject: |
|
|
Call it mind reading, but OceanMachine understood what I meant - which Sean so nicely pointed out. Thank you for the reply HotKeyIt - here is a shorted example: | Code: | var := 123 ;Int
var2 := "123" ;Str
SetFormat,IntegerFast,Hex
var:=var, var2:=var2
MsgBox,% var "`n" var2 |
EDIT - changed from Integer to IntegerFast per Lexikos' recommendation. _________________
- in case I forgot to smile
Basic Webpage Controls
COM Object Reference
Last edited by jethrow on Mon Mar 15, 2010 12:39 pm; edited 1 time in total |
|
| Back to top |
|
 |
|