Create named arrays from rows Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
A_Perry_1984
Posts: 76
Joined: 07 Dec 2018, 12:08

Create named arrays from rows

27 Nov 2019, 10:52

I have been trying to figure this out. I have a list and want to create arrays from every line, but I want also to name the array for each line after the first column. I am just not sure what I am missing here.

Code: Select all

#SingleInstance, Force
TaskX := []

List =
(
ListA	Info	more Info
ListB	Data	more Data
)

Loop, parse, List, `n
{	

	TaskX%A_Index% := StrSplit(A_LoopField, "`t").1 ; Array name is the first column
	TaskX[A_Index] := StrSplit(A_LoopField, "`t") ;Create an array from each variable name
	
}
For k, v in ListA
	MsgBox, 0x1000,, % v

*esc::
ExitApp


User avatar
Chunjee
Posts: 523
Joined: 18 Apr 2014, 19:05
GitHub: Chunjee

Re: Create named arrays from rows

27 Nov 2019, 11:06

Not sure if this matches the exact output you are looking for. output is ["ListA":"Info more Info", "ListB":"Data more Data"]

Code: Select all

List =
(
ListA	Info	more Info
ListB	Data	more Data
)

TaskX := []
Loop, parse, List, `n
{	
    elementArr := StrSplit(A_LoopField, "`t")
	TaskX[elementArr[1]] :=  elementArr[2] " " elementArr[3]
}
; => ["ListA":"Info more Info", "ListB":"Data more Data"]
ExitApp
A_Perry_1984
Posts: 76
Joined: 07 Dec 2018, 12:08

Re: Create named arrays from rows

27 Nov 2019, 11:33

Problem is I actually don't know the number of rows.
A_Perry_1984
Posts: 76
Joined: 07 Dec 2018, 12:08

Re: Create named arrays from rows

27 Nov 2019, 11:51

Sorry. My initial post wasn't exactly clear. I was just using the list above as an example. I am actually reading from a csv file with an indeterminate number of lines. When it reads a line, I want each line to be stored as an array but have that array named after the first column. So basically ListA would be it's own array containing everything on that line as elements.
User avatar
Xtra
Posts: 1770
Joined: 02 Oct 2015, 12:15

Re: Create named arrays from rows

27 Nov 2019, 12:21

Code: Select all

#NoEnv
#SingleInstance, Force

TaskX := []

List =
(
ListA	Info	more Info
ListB	Data	more Data
)

Loop, Parse, List, `n, `r
{	
    row := A_LoopField
	Loop, Parse, row, `t
	    if (A_Index = 1)
		    TaskX[key := A_LoopField] := []
		else
		    TaskX[key].push(A_LoopField)
}

for each, arr in TaskX
	for i, value in arr
	    MsgBox, 0x1000, % each, % "index " . i . "`nvalue " . value

ExitApp
User avatar
Chunjee
Posts: 523
Joined: 18 Apr 2014, 19:05
GitHub: Chunjee

Re: Create named arrays from rows

27 Nov 2019, 12:22

You still didn't say how you would like the output to look like.


How I would do it:

Code: Select all

A := new biga()
List =
(
ListA	Info	more Info
ListB	Data	more Data
)
TaskX := []

Loop, parse, List, `n
{	
    elementArr := StrSplit(A_LoopField, "`t")
	TaskX[A.head(elementArr)] := (A.tail(elementArr))
}
clipboard := A.printObj(TaskX)
; => ["ListA": [1:"Info", 2:"more Info"], "ListB": [1:"Data", 2:"more Data"]]
ExitApp
requires https://www.npmjs.com/package/biga.ahk
User avatar
flyingDman
Posts: 757
Joined: 29 Sep 2013, 19:01

Re: Create named arrays from rows  Topic is solved

27 Nov 2019, 16:34

A_Perry_1984 wrote:
27 Nov 2019, 11:51
Sorry. My initial post wasn't exactly clear. I was just using the list above as an example. I am actually reading from a csv file with an indeterminate number of lines. When it reads a line, I want each line to be stored as an array but have that array named after the first column. So basically ListA would be it's own array containing everything on that line as elements.
So array ListA contains 3 elements (ListA, Info, more Info) and array ListB contains 3 elements (ListB,Data,more Data).
Try this:

Code: Select all

List =
(
ListA	Info	more Info
ListB	Data	more Data
)

for a,b in strsplit(list,"`n","`r")
	tmp := strsplit(b,"`t"),z := tmp.1, %z% := tmp             ;or z := tmp.RemoveAt(1) to exclude ListX from the arrays

msgbox % listA.1 "`t" listA.2 "`t" listA.3
msgbox % listB.1 "`t" listB.2 "`t" listB.3

A_Perry_1984
Posts: 76
Joined: 07 Dec 2018, 12:08

Re: Create named arrays from rows

30 Nov 2019, 10:42

Okay, mind blown here. Thank you.

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], dentopolis, haichen, RubbeH, Xtra and 67 guests