AutoHotkey Community

It is currently May 27th, 2012, 9:12 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: November 18th, 2011, 5:27 pm 
Offline

Joined: October 13th, 2009, 10:09 pm
Posts: 1389
See code below:
Code:
x := "+5"
x := test(x)
MsgBox % x
test(arg)
{
   ;Uncomment code below to change "+5" to 5 ?
   while(someinvalidvar != arg)
   {
      someinvalidvar := arg
   }
   return arg
}
The combination of that while loop and the assignment will make AHK treat the content as a number rather then a string. Replacing while with if or using a break rather than the assignment in the loop won't show this behavior. Assigning a value to "someinvalidvar" doesn't have any effect.

[Moved from Wish List. ~jaco0646]


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 18th, 2011, 10:48 pm 
Offline

Joined: December 26th, 2010, 7:40 pm
Posts: 4172
Location: Awesometown, USA
Happens in until loops
Code:
x := "+5"
x := test(x)
MsgBox % x
test(arg)
{
   ;Uncomment code below to change "+5" to 5 ?
   Loop
   {
      someinvalidvar := arg
   }
   until someinvalidvar = arg
   return arg
}
And their if...break equivalent:
Code:
x := "+5"
x := test(x)
MsgBox % x
test(arg)
{
   ;Uncomment code below to change "+5" to 5 ?
   Loop
   {
      someinvalidvar := arg
      if (someinvalidvar = arg)
         break
   }
 
   return arg
}

but not if break is executed directly:
Code:
x := "+5"
x := test(x)
MsgBox % x
test(arg)
{
   ;Uncomment code below to change "+5" to 5 ?
   Loop
   {
      someinvalidvar := arg
    ; if (someinvalidvar = arg)
         break
   }
 
   return arg
}

leading me to believe that the problem is in the comparison, but it doesn't seem to be that simple:
Code:
x := "+5"
If (x != s)
   s := x
MsgBox % x
so it seems that it is comparison within a function:
Code:
x := "+5"
x := test(x)
MsgBox % x
test(arg)
{
      someinvalidvar := arg
      if (someinvalidvar = arg)
         Sleep -1 
      return arg
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 19th, 2011, 12:08 am 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7503
Location: Australia
I'm fairly certain this problem has existed since object support was added over two years ago, so how is it that two instances of it pop up in one week?
I wrote:
It is a design flaw which does not affect v2.
Source: Arrays and vars containing num-strings with leading zeros

In this case, the comparison of someinvalidvar ("+5") to arg ("+5") is performed numerically, which causes a pure number to be cached in both variables. When the variable is returned, it contains both a numeric string and a pure integer, but only one can be returned, and the pure integer takes precedence.

In v2 a distinction is made beween pure numbers and numeric strings, so the cache only works one-way: assign a pure number and populate the string buffer from that. If a variable contains a pure number, it must've been assigned a pure number. If it also contains a string, that doesn't matter since converting the pure number to a string will always yield the same result.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 19th, 2011, 12:26 pm 
Offline

Joined: October 13th, 2009, 10:09 pm
Posts: 1389
Coincidence I suppose, that's how statistics work ;)

Why is it that it doesn't happen with an if instead of a while?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 19th, 2011, 2:56 pm 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7503
Location: Australia
Which command you use and where you use it only affect the result because the values you are passing to the comparison operator are different.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 3 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