Problem creating array from string Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
john-arvid
Posts: 3
Joined: 07 Apr 2019, 08:38

Problem creating array from string

07 Apr 2019, 11:14

Hi,

I have tried some different approaches to this but I can't get it to work.

I have a string with many names delimited by a comma.
This string is then being parsed and pushed into a array.

The problem is that I can't get the information into the array. (I think)

This is the code:

Code: Select all

person := []
names := "alice,bob"

Loop, parse, names, `,
{
  ; This messagebox shows the data as it should
  MsgBox, Name number %A_Index% is %A_LoopField%
  person.Push(%A_LoopField%)
}

for index, element in person
{
  ;This messagebox does not show the data at all. I shows the correct index number, but not any content
  MsgBox % "Element number " . index . " is " . element
}
As the comments say, the messagebox in the loop shows the content ok, but the messagebox in the for loop does not.

What am I doing wrong?
Klarion
Posts: 176
Joined: 26 Mar 2019, 10:02

Re: Problem creating array from string

07 Apr 2019, 11:43

Code: Select all

person.Push(A_LoopField)
User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Problem creating array from string

07 Apr 2019, 12:02

Cheers.

Code: Select all

person := StrSplit("alice,bob", ",")
MsgBox, % person.1
MsgBox, % person[2]
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
john-arvid
Posts: 3
Joined: 07 Apr 2019, 08:38

Re: Problem creating array from string

07 Apr 2019, 12:04

Klarion wrote:
07 Apr 2019, 11:43

Code: Select all

person.Push(A_LoopField)
Thank you very much. Did not catch that when reading about variables.
Klarion
Posts: 176
Joined: 26 Mar 2019, 10:02

Re: Problem creating array from string

07 Apr 2019, 12:28

StrSplit is the better one
john-arvid
Posts: 3
Joined: 07 Apr 2019, 08:38

Re: Problem creating array from string

07 Apr 2019, 13:23

Ok, could I also use it when using multi dimensional arrays?
That is what I am doing in my next revision of the code, person[1,1] := name; person[1,2] := age etc
User avatar
lvalkov
Posts: 39
Joined: 08 Mar 2019, 16:39

Re: Problem creating array from string

07 Apr 2019, 15:00

Code: Select all

Person := []
Person.Push(StrSplit("Alice,30", ","))
Person.Push(StrSplit("Bob,40", ","))

Return to “Ask For Help”

Who is online

Users browsing this forum: Albireo, Bing [Bot], synnapt, Xtra and 80 guests