AutoHotkey Community

It is currently May 27th, 2012, 5:35 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 19 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: March 14th, 2010, 6:21 am 
Offline
User avatar

Joined: May 24th, 2009, 5:35 am
Posts: 2099
Location: Iowa, USA
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?

_________________
Image
Recommended: AutoHotkey_L
Basic Webpage Controls


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 14th, 2010, 7:00 am 
Code:
MsgBox % 123/1
MsgBox % "123"/1
?


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 14th, 2010, 7:44 am 
Offline
User avatar

Joined: May 24th, 2009, 5:35 am
Posts: 2099
Location: Iowa, USA
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

_________________
Image
Recommended: AutoHotkey_L
Basic Webpage Controls


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 14th, 2010, 8:45 am 
Code:
var = 123
var2 = "123"
MyFunc(var)
MyFunc(var2)

MyFunc(var) {
   If va is integer
      MsgBox % var
   Return
   }
?


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 14th, 2010, 8:46 am 
Ouch! :shock:

Code:
[...]
If var is integer
[...]


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 14th, 2010, 8:56 am 
Offline

Joined: October 15th, 2007, 3:10 pm
Posts: 790
Location: England
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 14th, 2010, 12:42 pm 
Offline

Joined: February 12th, 2007, 7:54 am
Posts: 2462
AHK doesn't expose any way for a script to distinguish the two.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 14th, 2010, 12:57 pm 
Offline

Joined: November 7th, 2006, 9:47 pm
Posts: 1934
Location: Germany
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! <--


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 14th, 2010, 5:25 pm 
Offline

Joined: October 7th, 2006, 4:50 pm
Posts: 3157
Location: MN, USA
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 March 14th, 2010, 5:29 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 14th, 2010, 5:28 pm 
Offline

Joined: November 7th, 2006, 9:47 pm
Posts: 1934
Location: Germany
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! <--


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 14th, 2010, 5:33 pm 
Offline

Joined: October 7th, 2006, 4:50 pm
Posts: 3157
Location: MN, USA
Tuncay wrote:
the user make extra effort
jethrow was already doing that (just using the wrong operator).


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 14th, 2010, 5:38 pm 
Offline

Joined: November 7th, 2006, 9:47 pm
Posts: 1934
Location: Germany
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! <--


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 14th, 2010, 5:42 pm 
Offline

Joined: February 12th, 2007, 7:54 am
Posts: 2462
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 14th, 2010, 6:18 pm 
Offline

Joined: June 18th, 2008, 8:36 am
Posts: 4923
Location: AHK Forum
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 :wink:


Last edited by HotKeyIt on March 15th, 2010, 2:30 pm, edited 2 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 14th, 2010, 7:19 pm 
Offline
User avatar

Joined: May 24th, 2009, 5:35 am
Posts: 2099
Location: Iowa, USA
Call it mind reading, but OceanMachine understood what I meant - which Sean so nicely pointed out. Thank you for the reply HotKeyIt :D - 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.

_________________
Image
Recommended: AutoHotkey_L
Basic Webpage Controls


Last edited by jethrow on March 15th, 2010, 1:39 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 19 posts ]  Go to page 1, 2  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], dra, HotkeyStick, mrhobbeys, rbrtryn, XstatyK and 60 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