Issue with Building an Array Using a Loop Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Loop
Posts: 171
Joined: 07 Jan 2019, 14:51

Issue with Building an Array Using a Loop

Post by Loop » 09 Apr 2024, 17:23

Hello everyone,

I've been trying to build an array using a loop, but I keep encountering an error: "Invalid index." Here's how my loop looks like:

Code: Select all

#Requires Autohotkey v2.0
#SingleInstance Force


Ier := []
Aer := ["a", "b", "c", "d", "e", "f","g","h"]
Loop 8
{

   Ier[A_index] := []
   Ier[A_Index].Push(Aer[A_index])
}

MsgBox Ier[2][1]


Could you please help me identify what I'm doing wrong?

My array should look like this at the end

Code: Select all

Ier[[a],[b],[c]]
Thank you in advance.

ntepa
Posts: 439
Joined: 19 Oct 2022, 20:52

Re: Issue with Building an Array Using a Loop  Topic is solved

Post by ntepa » 09 Apr 2024, 17:55

A valid array index needs to be less than or equal to the array length. Ier := [] creates an array with a length of 0.
In the first iteration of the loop, it tries to set Ier[1] := []. 1 is an invalid index because it's greater than Ier.length.
You can set the length after creating Ier:

Code: Select all

#Requires Autohotkey v2.0
#SingleInstance Force


Ier := []
Ier.Length := 8
Aer := ["a", "b", "c", "d", "e", "f","g","h"]
Loop 8
{

   Ier[A_index] := []
   Ier[A_Index].Push(Aer[A_index])
}

MsgBox Ier[2][1]
Or push an array instead:

Code: Select all

#Requires Autohotkey v2.0
#SingleInstance Force


Ier := []
Aer := ["a", "b", "c", "d", "e", "f","g","h"]
Loop 8
{

   Ier.Push([])
   Ier[A_Index].Push(Aer[A_index])
}

MsgBox Ier[2][1]

niCode
Posts: 320
Joined: 17 Oct 2022, 22:09

Re: Issue with Building an Array Using a Loop

Post by niCode » 09 Apr 2024, 17:59

What ntepa said. Here's just some more ways of envisioning it.

Code: Select all

Aer := ["a", "b", "c", "d", "e", "f","g","h"]
Ier := []
Ier.Length := Aer.Length

Loop Aer.Length
{
    Ier[A_index] := []
    Ier[A_Index].Push(Aer[A_index])
}

MsgBox(Ier[2][1])
or

Code: Select all

Ier := []
Aer := ["a", "b", "c", "d", "e", "f","g","h"]

Loop Aer.Length
{
    Ier.Push([])
    Ier[A_Index].Push(Aer[A_Index])
}

MsgBox(Ier[2][1])
or

Code: Select all

Ier := []

for index, value in ["a", "b", "c", "d", "e", "f","g","h"]
{
    Ier.Push([value])
}

MsgBox(Ier[2][1])

teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: Issue with Building an Array Using a Loop

Post by teadrinker » 09 Apr 2024, 19:21

Without a loop:

Code: Select all

Aer := ["a", "b", "c", "d", "e", "f","g","h"]
Ier := [((e, _, &v) => e(&v) && v := [v]).Bind(Aer.__Enum(1), &v)*]
MsgBox Ier[2][1]
:)

Loop
Posts: 171
Joined: 07 Jan 2019, 14:51

Re: Issue with Building an Array Using a Loop

Post by Loop » 10 Apr 2024, 09:04

Thank you for your responses.
But is this new in AHK2?

teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: Issue with Building an Array Using a Loop

Post by teadrinker » 10 Apr 2024, 14:27

I'm not sure exactly what you mean, but yes, arrays are arranged differently in the second version. In the first version there was no difference between obj := [] and obj := {}. See this post.

Loop
Posts: 171
Joined: 07 Jan 2019, 14:51

Re: Issue with Building an Array Using a Loop

Post by Loop » 10 Apr 2024, 16:49

Thank you, I meant that previously you didn't have to assign a length to an index-based array. Is ier.Length := 8 ;>>> new?

teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: Issue with Building an Array Using a Loop

Post by teadrinker » 10 Apr 2024, 17:30

Yes, in the first version arrays didn't have a "Length" property at all, they had a Length() method that returned the maximum positive numeric key:

Code: Select all

#Requires AutoHotkey v1
arr := []
arr.a := "a"
arr[-1] := "b"
arr[5] := "c"
MsgBox % "length: " . arr.Length()
for k, v in arr {
    MsgBox % "key: " . k . "`nvalue: " . v
}
In the second version, you can't write like this:

Code: Select all

#Requires AutoHotkey v2

arr := []
arr[1] := 'test'
because initially there is no key with number 1 in the empty array.
But you can do it this way:

Code: Select all

#Requires AutoHotkey v2

arr := []
arr.Push('test')
In this case, the keys will be created automatically.

Loop
Posts: 171
Joined: 07 Jan 2019, 14:51

Re: Issue with Building an Array Using a Loop

Post by Loop » 11 Apr 2024, 04:52

Thank you for the explanation

Rohwedder
Posts: 7774
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Issue with Building an Array Using a Loop

Post by Rohwedder » 11 Apr 2024, 09:35

Hallo @teadrinker,
in your opinion, what would be the best translation of your v1 array example to v2?

teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: Issue with Building an Array Using a Loop

Post by teadrinker » 12 Apr 2024, 15:16

@Rohwedder
I think the closest translation would be this:

Code: Select all

#Requires AutoHotkey v2

obj := {}
obj.a := 'a'
obj.%-1% := 'b'
obj.5 := 'c'
for k, v in obj.OwnProps() {
    MsgBox 'key: ' . k . '`nvalue: ' . v
}

Post Reply

Return to “Ask for Help (v2)”