Array Syntax Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
MaxAstro
Posts: 557
Joined: 05 Oct 2016, 13:00

Array Syntax

26 Apr 2018, 14:42

I am so, so bad at array syntax. I can never get to the point that it actually makes sense to me in a way that works consistently. Can someone please tell me what the correct syntax for this is?

Code: Select all

Col1Array := { 1 : "" }, Col2Array := { 1 : "" }, Col3Array := { 1 : "" }, Col4Array := { 1 : "" }, Col5Array := { 1 : "" }, Col6Array := { 1 : "" }
2DArray := {Col1 : Col1Array, Col2 : Col2Array, Col3 : Col3Array, Col4 : Col4Array, Col5 : Col5Array, Col6 : Col6Array}

Loop, 6
	MsgBox % 2DArray[Col%A_Index%].Length()
2DArray.Col1.Length() works fine, but I need to be able to variably assign which column I'm looking at...

My use case is this: I'm building a 2D array out of various information read out of a text file, storing information like names, phone numbers, email addresses, etc. The information is all out of order in the text file, so I want to read it into a 2D array such that Col1Array contains all of the names, Col2Array all of the phone numbers, etc, and then use that to write to a CSV file with the information formatted correctly and in order. So when it's parsing through the text file, each time it finds a specific kind of information, it needs to add that information to the end of the array of that type.
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: Array Syntax  Topic is solved

26 Apr 2018, 14:54

Hi MaxAstro,

By preceding the first parameter of your MsgBox command with a percent sign and a space you're correctly trying to force an expression to use it in command parameter that does not directly support it. Yet, using an expression literal strings must be enclosed in double quotes to distinguish them from variable. Col is intended to be a string, it must be enclosed in quotes:

Code: Select all

(a := []).test7 := "blabla" . 1000
MsgBox % a["test" . 6 + 1]
Hope this helps.
my scripts
User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: Array Syntax

26 Apr 2018, 14:58

What about this?

Code: Select all

2DArray := []
loop, 6
    2DArray.push({ "Col" . A_Index: { 1: ""} })

Loop, 6
	MsgBox % 2DArray[A_Index, "Col" . A_Index].Length()
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Array Syntax

26 Apr 2018, 17:05

Code: Select all

Data := {"names" : []
		, "phones" : []
		, "addresses" : []}

/*
alternatively
Data := {}
Data.names := []
Data.phones := []
Data.addresses := []
*/

Data.names.push("John")
Data.names.push("Mary")
Data.names.push("Peter")

Data.phones.push(1234567890)
Data.phones.push(666)

Data.addresses.push("somewhere")
Data.addresses.push("Unoriginality Ave.")
Data.addresses.push("the white house")

for categoryName, categoryData in Data {
	for index, entry in categoryData {
		MsgBox, % "Picking from: " . categoryName
			. "`r`n" . "Entry: " . entry
	}
}  

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], onurcoban and 366 guests