AutoHotkey Community

It is currently May 27th, 2012, 11:34 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: local array
PostPosted: October 27th, 2009, 9:54 pm 
Offline

Joined: January 31st, 2005, 9:50 am
Posts: 3910
Location: Bremen, Germany
Hello,

I'm using AHK 1.0.48.05 and this doesn't seem to work as I expect:
Code:
t = global
f()
msgbox, 2 %t% %s3% <= should say "2 global"

f(){
  local s0 
  string = local 
  StringSplit, s, string 
  Msgbox, 1 %t% %s3% <= should say "1 global c"
}
The manual says on local arrays
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 (...). Conversely, if the first element has been declared global, a global array is created. The first element for StringSplit is ArrayName0.
Is this a bug or did I do something wrong?

_________________
Ciao
toralf
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 28th, 2009, 12:57 am 
Offline

Joined: October 7th, 2006, 4:50 pm
Posts: 3157
Location: MN, USA
AHK Help File wrote:
Common source of confusion: Any non-dynamic reference to a variable creates that variable the moment the script launches... when used inside a function MsgBox %Array1% creates Array1 as one of the function's locals the moment the script launches (unless assume-global is in effect).

In your function, %s3% is global. To see the contents of the local, reference it dynamically.
Code:
t = global
f()
msgbox, 2 %t% %s3% <= should say "2 global"

f(){
  local s0
  string = local ;<--this is a global variable
  StringSplit, s, string
  var = 3
  Msgbox,% "1 " t " " s%var% " <= should say ""1 global c"""
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 28th, 2009, 7:45 pm 
Offline

Joined: January 31st, 2005, 9:50 am
Posts: 3910
Location: Bremen, Germany
Thank you jaco0646,

I could verify what you wrote, with the code below. Even though I do not find it clear.
Code:
t = global
i = 3
f()
; Msgbox,         2 %t% %s3% <= expected '2 global', and it DOES
; Msgbox,   % "2 " t " " s3 "<= expected '2 global', and it DOES"
Msgbox, % "2 " t " " s%i% "<= expected '2 global', and it DOES"

f(){
  local s0
  string = local
  StringSplit, s, string
ListVars
;   Msgbox,         1 %t% %s3% <= expected '1 global c', but it shows '1 global',`nbecause the s3 var is called as a non-dynamic reference and created as global but empty
;   Msgbox,   % "1 " t " " s3 "<= expected '1 global c', but it shows '1 global',`nbecause the s3 var is called as a non-dynamic reference and created as global but empty"
  Msgbox, % "1 " t " " s%i% "<= expected '1 global c', and it DOES"
}


1) I can accept that
Quote:
Common source of confusion: Any non-dynamic reference to a variable creates that variable the moment the script launches... when used inside a function MsgBox %Array1% creates Array1 as one of the function's locals the moment the script launches (unless assume-global is in effect).
Hence in my code the assume-global is in effect all non-dynamic reference are created global.

2) But when I read
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 (...). Conversely, if the first element has been declared global, a global array is created. The first element for StringSplit is ArrayName0.
This is exactly what I did in my code, therefore I assumed that this overrides the Common source of confusion with assume-global and I would reference a local variable.

Is there a way to change this AHK behavior?

_________________
Ciao
toralf
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 28th, 2009, 10:42 pm 
Offline

Joined: October 7th, 2006, 4:50 pm
Posts: 3157
Location: MN, USA
At first, it seems those two Help File quotes contradict eachother, but they are both correct. One behavior does not override the other; both are happening in your code. You are creating a local array, and you are creating a global variable. So you end up with a local variable s3 that is part of the array, and also a global variable s3 that is not assigned any value. The way you reference s3 (dynamically or non-dynamically) determines which one you get. This code may be a bit more clear.
Code:
s3 = global
f()
MsgBox,,3, %s3%

f(){
 local s0 ;<--assume-global mode is now in effect
 string = global
 StringSplit, s, string ;<--create a local array (because s0 is local)
 var = 3
 MsgBox,,1, s3 = %s3% ;<--a non-dynamic variable references the global s3
 MsgBox,,2,% "s%3% = " s%var% ;<--a dynamic variable references the local s3
}

toralf wrote:
Is there a way to change this AHK behavior?
The behavior changes depending on whether your function is assume-global or assume-local. Comment out the first line of the function and note the difference: non-dynamic variable references become local.


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

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot] and 17 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