Jump to content
Sign in to follow this  
omerbn

array scope bug?

Recommended Posts

omerbn

hi guys,

I've been trying to find out what am I doing worng.

It looks like there is a variable scope bug?

take a look:

Dim $count = 0

Dim $arr[1] ;declaring an array of size 1

While 1=1

$soft_key = RegEnumKey ($main_key, $count)

; this is the exit condition

If @error <> 0 then

ExitLoop

EndIf

$soft_name = RegRead ($main_key & "\" & $soft_key, "Displayname")

$soft_name = StringReplace ($soft_name, " (remove only)", "")

If $soft_name <> "" Then

$arr[uBound($arr)-1] = "s"

reDim $arr[uBound($arr)+1]

EndIf

$count += 1

Wend

I get this error message:

$arr[0] = "s"

$arr^ERROR

Error: Subscript used with non-Array variable

Can you please advise?

Thx,

Omerbn

Share this post


Link to post
Share on other sites
PsaltyDS

hi guys,

I've been trying to find out what am I doing worng.

It looks like there is a variable scope bug?

take a look:

Dim $count = 0

Dim $arr[1] ;declaring an array of size 1

While 1=1

$soft_key = RegEnumKey ($main_key, $count)

; this is the exit condition

If @error <> 0 then

ExitLoop

EndIf

$soft_name = RegRead ($main_key & "\" & $soft_key, "Displayname")

$soft_name = StringReplace ($soft_name, " (remove only)", "")

If $soft_name <> "" Then

$arr[uBound($arr)-1] = "s"

reDim $arr[uBound($arr)+1]

EndIf

$count += 1

Wend

I get this error message:

$arr[0] = "s"

$arr^ERROR

Error: Subscript used with non-Array variable

Can you please advise?

Thx,

Omerbn

The registry stuff is a distractor, why include it in the example? This works fine:
#include <Array.au3>; Only for _ArrayDisplay()

Dim $count = 0
Dim $arr[1]
While 1 = 1
    If $count >= 10 Then ExitLoop
    $arr[UBound($arr) - 1] = "s"
    ReDim $arr[UBound($arr) + 1]
    $count += 1
WEnd

_ArrayDisplay($arr, "$arr")

Now, how big is $arr getting before you get this error in your version? Or, does it error on the first pass? Throw in some ConsoleWrite()'s or something to monitor what is happening while it runs.

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
omerbn

The registry stuff is a distractor, why include it in the example? This works fine:

#include <Array.au3>; Only for _ArrayDisplay()

Dim $count = 0
Dim $arr[1]
While 1 = 1
    If $count >= 10 Then ExitLoop
    $arr[UBound($arr) - 1] = "s"
    ReDim $arr[UBound($arr) + 1]
    $count += 1
WEnd

_ArrayDisplay($arr, "$arr")

Now, how big is $arr getting before you get this error in your version? Or, does it error on the first pass? Throw in some ConsoleWrite()'s or something to monitor what is happening while it runs.

:)

Hi again,

First, thanx for your quick reply.

Second, You example works great but I have another question: how can I "pass" an array as a parameter to a function?

e.g.:

Dim $arr[12]

$arr[0] = 1

$arr[1] = 2

$arr = enlargeArr($arr)

function enlargeArr($arr)

{

reDim $arr[uBound($arr)+1]

return $arr

}

It doesn't work for me :P

I get an error message which writes that $arr is not an array variable.

Share this post


Link to post
Share on other sites
Malkey

Hi again,

First, thanx for your quick reply.

Second, You example works great but I have another question: how can I "pass" an array as a parameter to a function?

e.g.:

Dim $arr[12]

$arr[0] = 1

$arr[1] = 2

$arr = enlargeArr($arr)

function enlargeArr($arr)

{

reDim $arr[uBound($arr)+1]

return $arr

}

It doesn't work for me :)

I get an error message which writes that $arr is not an array variable.

Dim $arr[12]
$arr[0] = 1
$arr[1] = 2
$arr = enlargeArr($arr)
MsgBox(0,"","$array[" & UBound($arr) &"]")

Func enlargeArr($arr)
    reDim $arr[UBound($arr)+1]
    return $arr
EndFunc

Share this post


Link to post
Share on other sites
MHz

I would use ByRef to pass the array into the function to be directly manipulated or use it globally by not using it as a parameter but as a global array variable and the function should see and use it.

Using ByRef example

; declare an array with 1 element
Global $test[1] = [1]

; add another element using ByRef
; so the array variable itself is changed
_ArrayAddIndex($test, 1)

; assign a value to the 2nd element
$test[1] = 2

; show the result
For $i = 0 To UBound($test) - 1
    MsgBox(0x40000, '$test[' & $i & ']', $test[$i])
Next

; and once again add 2 more elements
_ArrayAddIndex($test, 2)

; assign a value to the 3rd and 4th elements
$test[2] = 3
$test[3] = 4

; show the result
For $i = 0 To UBound($test) - 1
    MsgBox(0x40000, '$test[' & $i & ']', $test[$i])
Next

Exit

Func _ArrayAddIndex(ByRef $array, $index = 1)
    ; resize an array by increasing the size
    ; with the amount stored in $index
    ReDim $array[UBound($array) + $index]
EndFunc

:)

Share this post


Link to post
Share on other sites
omerbn

I would use ByRef to pass the array into the function to be directly manipulated or use it globally by not using it as a parameter but as a global array variable and the function should see and use it.

Using ByRef example

; declare an array with 1 element
Global $test[1] = [1]

; add another element using ByRef
; so the array variable itself is changed
_ArrayAddIndex($test, 1)

; assign a value to the 2nd element
$test[1] = 2

; show the result
For $i = 0 To UBound($test) - 1
    MsgBox(0x40000, '$test[' & $i & ']', $test[$i])
Next

; and once again add 2 more elements
_ArrayAddIndex($test, 2)

; assign a value to the 3rd and 4th elements
$test[2] = 3
$test[3] = 4

; show the result
For $i = 0 To UBound($test) - 1
    MsgBox(0x40000, '$test[' & $i & ']', $test[$i])
Next

Exit

Func _ArrayAddIndex(ByRef $array, $index = 1)
    ; resize an array by increasing the size
    ; with the amount stored in $index
    ReDim $array[UBound($array) + $index]
EndFunc

:)

Thanks ALOT man!!

You really helped me!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×