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 

I think I have found a bug in arrays

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
corsair992



Joined: 12 Jan 2010
Posts: 8
Location: Pakistan

PostPosted: Tue Jan 12, 2010 3:09 pm    Post subject: I think I have found a bug in arrays Reply with quote

I tried to use a global array in a function in assume-global mode, providing it with a local variable. The local variable was also an element of an array that was created by SplitString. I had declared this array local at the beginning of the function, by writing its name with a 0 at the end. The elements of this local array show their values perfectly, but when one element of it was used inside a global array, the value was blank.

Just to test, I made this local array global. Everything worked perfectly then. This seems to be a bug to me. My code is pretty involved, but I think this might be reproducible.

I am sorry for any mistakes in terminology. I have tried to describe it as best as I could.
Back to top
View user's profile Send private message
sinkfaze



Joined: 18 Mar 2008
Posts: 5043
Location: the tunnel(?=light)

PostPosted: Tue Jan 12, 2010 3:24 pm    Post subject: Reply with quote

Have you searched the forum for similar topics on this potential bug? I think what you're seeing might be what's being described here.
_________________
Try Quick Search for Autohotkey or see the tutorial for newbies.
Back to top
View user's profile Send private message Send e-mail
corsair992



Joined: 12 Jan 2010
Posts: 8
Location: Pakistan

PostPosted: Tue Jan 12, 2010 3:56 pm    Post subject: Reply with quote

Thanks for the link! This is similar to mine, but this is not what is happening with me. In my script, the global array is declared outside of the relevant function, and is not redeclared in the function as a local variable, as happened in that topic. Therefore, the problem seems to lie somewhere else.

But thanks for the response.


Last edited by corsair992 on Tue Jan 12, 2010 4:47 pm; edited 1 time in total
Back to top
View user's profile Send private message
HotKeyIt



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

PostPosted: Tue Jan 12, 2010 4:18 pm    Post subject: Re: I think I have found a bug in arrays Reply with quote

Quote:
The local variable was also an element of an array that was created by SplitString. I had declared this array local at the beginning of the function, by writing its name with a 0 at the end.


When your function is assume global, how does your StringSplit create a local variable?
It is not enough to declare the name and 0 only, you would need to declare all numbers Exclamation

Not sure how you use your local array, but when function finishes all local variables are cleared Exclamation

Can you give an example Question
_________________
AHK_H (2alpha) AHF TT _Struct WatchDir Yaml _Input ObjTree RapidHotkey DynaRun Wink
Back to top
View user's profile Send private message
corsair992



Joined: 12 Jan 2010
Posts: 8
Location: Pakistan

PostPosted: Tue Jan 12, 2010 4:23 pm    Post subject: Reply with quote

Hi, HotKeyIt! Quote from the manual

Quote:
For commands that create arrays (such as StringSplit), the resulting array is local if the assume-global mode is not in effect or if the array's first element has been declared as a local variable (this is also true if one of the function's parameters is passed -- even if that parameter is ByRef -- because parameters are similar to local variables). Conversely, if the first element has been declared global, a global array is created. The first element for StringSplit is ArrayName0. For other array-creating commands such as WinGet List, the first element is ArrayName (i.e. without the number).


As you can see, to make the array that is created by StringSplit local in a function with assume-global mode, it is only necessary to declare the name+0 to be local. That is what I did.
Back to top
View user's profile Send private message
Peter



Joined: 30 Dec 2005
Posts: 448

PostPosted: Tue Jan 12, 2010 5:39 pm    Post subject: Reply with quote

HotKeyIt wrote:
Can you give an example Question
@corsair992: A simple example avoids any misunderstandings about the problem. I was waiting too Smile . (But now I can't check before tomorrow)
Back to top
View user's profile Send private message
corsair992



Joined: 12 Jan 2010
Posts: 8
Location: Pakistan

PostPosted: Tue Jan 12, 2010 7:27 pm    Post subject: Reply with quote

Ok, I messed around tried to reproduce and isolate my problem, and this is what I found out. If a local array is made by SplitString in a function with assume-global mode according to the way described in the manual (see my above post), then while the array is local, any subsequent mention of any element of it will declare and refer to a new global variable of that name.

This is a short debug script I wrote while messing with this.

Code:
#Persistent

GlobalArray1 := 45
GlobalArray2 := 45
GlobalArray3 := 45

TestString := "123"

TestFunction()

TestFunction()
{
  Local LocalArray0, a
  StringSplit LocalArray, TestString
  a := LocalArray1
  ListVars
}


You will see that the command "a := LocalArray1" creates a new global LocalArray1 and refers to it.

As far as I can see, this should not be happening. What is the use of making a local array if you can't refer to any element in it, after all?
Back to top
View user's profile Send private message
HotKeyIt



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

PostPosted: Tue Jan 12, 2010 9:35 pm    Post subject: Reply with quote

This is because you have a variable in your function that is created when script starts, the variable is not declared to be local and it does not check whether this variable could be meant to be a local one.
Imagine there would be a variable LocalArray1000000, it would need to create 1 millions local variables, which would be senseless.
You just have to resolve the variable dynamically:
Code:
#Persistent

GlobalArray1 := 45
GlobalArray2 := 45
GlobalArray3 := 45

TestString := "123"

TestFunction(1)

TestFunction(arr)
{
  Local LocalArray0, a
  StringSplit LocalArray, TestString
  a := LocalArray%arr%
  ListVars
}

_________________
AHK_H (2alpha) AHF TT _Struct WatchDir Yaml _Input ObjTree RapidHotkey DynaRun Wink
Back to top
View user's profile Send private message
corsair992



Joined: 12 Jan 2010
Posts: 8
Location: Pakistan

PostPosted: Wed Jan 13, 2010 2:46 am    Post subject: Reply with quote

Thank you very much, HotkeyIt! You have made my day Very Happy
I still think Autokotkey needs to work on their arrays. This would not have happened with a normal array. However, at least this is not a bug.
Back to top
View user's profile Send private message
HotKeyIt



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

PostPosted: Wed Jan 13, 2010 6:38 am    Post subject: Reply with quote

corsair992 wrote:
Thank you very much, HotkeyIt! You have made my day Very Happy
I still think Autokotkey needs to work on their arrays. This would not have happened with a normal array. However, at least this is not a bug.


You should try objects in AutoHotkey_L.exe, these objects are much more of use than usual AutoHotkey array and offer some special features.
_________________
AHK_H (2alpha) AHF TT _Struct WatchDir Yaml _Input ObjTree RapidHotkey DynaRun Wink
Back to top
View user's profile Send private message
corsair992



Joined: 12 Jan 2010
Posts: 8
Location: Pakistan

PostPosted: Wed Jan 13, 2010 7:37 pm    Post subject: Reply with quote

HotKeyIt wrote:
You should try objects in AutoHotkey_L.exe, these objects are much more of use than usual AutoHotkey array and offer some special features.


Thanks for referring me to this. Some very nice functionality. Specifically, the #if expression will be very useful in my script, so I will probably be using this.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
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