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 

Distinguish between a String & Number
Goto page 1, 2  Next
 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
jethrow



Joined: 24 May 2009
Posts: 1907
Location: Iowa, USA

PostPosted: Sun Mar 14, 2010 5:21 am    Post subject: Distinguish between a String & Number Reply with quote

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?
_________________
Very Happy - in case I forgot to smile
Basic Webpage Controls
COM Object Reference
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
Murx
Guest





PostPosted: Sun Mar 14, 2010 6:00 am    Post subject: Reply with quote

Code:
MsgBox % 123/1
MsgBox % "123"/1
?
Back to top
jethrow



Joined: 24 May 2009
Posts: 1907
Location: Iowa, USA

PostPosted: Sun Mar 14, 2010 6:44 am    Post subject: Reply with quote

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

_________________
Very Happy - in case I forgot to smile
Basic Webpage Controls
COM Object Reference
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
Murx
Guest





PostPosted: Sun Mar 14, 2010 7:45 am    Post subject: Reply with quote

Code:
var = 123
var2 = "123"
MyFunc(var)
MyFunc(var2)

MyFunc(var) {
   If va is integer
      MsgBox % var
   Return
   }
?
Back to top
Murx
Guest





PostPosted: Sun Mar 14, 2010 7:46 am    Post subject: Reply with quote

Ouch! Shocked

Code:
[...]
If var is integer
[...]
Back to top
OceanMachine



Joined: 15 Oct 2007
Posts: 780
Location: England

PostPosted: Sun Mar 14, 2010 7:56 am    Post subject: Reply with quote

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
View user's profile Send private message
Sean



Joined: 12 Feb 2007
Posts: 2462

PostPosted: Sun Mar 14, 2010 11:42 am    Post subject: Reply with quote

AHK doesn't expose any way for a script to distinguish the two.
Back to top
View user's profile Send private message
Tuncay



Joined: 07 Nov 2006
Posts: 1886
Location: Germany

PostPosted: Sun Mar 14, 2010 11:57 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website
jaco0646



Joined: 07 Oct 2006
Posts: 3113
Location: MN, USA

PostPosted: Sun Mar 14, 2010 4:25 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Tuncay



Joined: 07 Nov 2006
Posts: 1886
Location: Germany

PostPosted: Sun Mar 14, 2010 4:28 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website
jaco0646



Joined: 07 Oct 2006
Posts: 3113
Location: MN, USA

PostPosted: Sun Mar 14, 2010 4:33 pm    Post subject: Reply with quote

Tuncay wrote:
the user make extra effort
jethrow was already doing that (just using the wrong operator).
Back to top
View user's profile Send private message Visit poster's website
Tuncay



Joined: 07 Nov 2006
Posts: 1886
Location: Germany

PostPosted: Sun Mar 14, 2010 4:38 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website
Sean



Joined: 12 Feb 2007
Posts: 2462

PostPosted: Sun Mar 14, 2010 4:42 pm    Post subject: Reply with quote

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
View user's profile Send private message
HotKeyIt



Joined: 18 Jun 2008
Posts: 4652
Location: AHK Forum

PostPosted: Sun Mar 14, 2010 5:18 pm    Post subject: Reply with quote

There is a way to distinguish Wink
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 Wink


Last edited by HotKeyIt on Mon Mar 15, 2010 1:30 pm; edited 2 times in total
Back to top
View user's profile Send private message
jethrow



Joined: 24 May 2009
Posts: 1907
Location: Iowa, USA

PostPosted: Sun Mar 14, 2010 6:19 pm    Post subject: Reply with quote

Call it mind reading, but OceanMachine understood what I meant - which Sean so nicely pointed out. Thank you for the reply HotKeyIt Very Happy - 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.
_________________
Very Happy - 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
View user's profile Send private message Visit poster's website Yahoo Messenger
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
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