using variable as array dynamic name

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
shkhoo
Posts: 4
Joined: 10 Dec 2018, 00:41

using variable as array dynamic name

Post by shkhoo » 06 Dec 2021, 01:16

I would like to create three arrays. For example array1, array 2, array 3 but It is not working with this code.
My final objective is to have something like this :=
array1(112, 333, 335, 66)
array2(55,99,646,886,75)
array3(77,215,4,10)

Code: Select all

f1::

loop, 3
    {
    array%a_index% := []
    }

loop, 3
    {
    loop,4
        {
        random, rnd, 0, 100
        array%a_index%.Push := rnd
        }
    }

for k, v in array1
    {
    msgbox % "k : " k ", v : " v
    }

return

esc::Reload

User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: using variable as array dynamic name

Post by Xtra » 06 Dec 2021, 01:27

Code: Select all

f1::

loop, 3
    {
        index := A_Index
        array%index% := []
    loop,4
        {
        random, rnd, 0, 100
        array%index%.push(rnd)
        }
    }

for k, v in array1
    {
    msgbox % "k : " k ", v : " v
    }

return

esc::Reload

shkhoo
Posts: 4
Joined: 10 Dec 2018, 00:41

Re: using variable as array dynamic name

Post by shkhoo » 06 Dec 2021, 01:42

i think i got it.

Code: Select all

f1::

loop, 3
    {
    array%a_index% := []
    }

loop, 3
    {
    loop,4
        {
        random, rnd, 0, 100
        array%a_index%.push(rnd)
        }
    }

for k, v in array1
    {
    msgbox % "array1 : " a_index "`r k : " k ", v : " v
    }

for k, v in array2
    {
    msgbox % "array2 : " a_index "`r k : " k ", v : " v
    }

for k, v in array3
    {
    msgbox % "array3 : " a_index "`r k : " k ", v : " v
    }

return

User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: using variable as array dynamic name

Post by Xtra » 06 Dec 2021, 01:56

change:

Code: Select all

loop, 3
    {
    loop,4
        {
        random, rnd, 0, 100
        array%a_index%.push(rnd)    ; There is no array4 
        }
    }
to:

Code: Select all

loop, 4    ; <---- Adjust as needed if you want 3 or 4 array elements.
    {
    loop,3
        {
        random, rnd, 0, 100
        array%a_index%.push(rnd)    ; When using a_index its using the number for the loop its in
        }
    }

Post Reply

Return to “Ask for Help (v1)”