Jump to content


Photo

Help with multidimensional arrays


  • Please log in to reply
6 replies to this topic

#1 neXt

neXt
  • Members
  • 533 posts

Posted 30 April 2012 - 04:04 PM

Here's what I'm trying to do:
1) I have several crews (AA-001, BB-002, CC-003, etc.)
2) Each crew has multiple job orders assigned to them
3) I need to fill an array with data, then extract it on a per crew basis.

It should be something like this as I load it:
db := {code: "AA-001", work_order: "1", work_order: "2", work_order: "3"}
db := {code: "BB-002", work_order: "10", work_order: "11", work_order: "12"}
Then I'd like to extract every work order number for "AA-001", and so on, for each crew.

I have no idea how to accomplish this :cry:

#2 sinkfaze

sinkfaze
  • Moderators
  • 6087 posts

Posted 30 April 2012 - 04:12 PM

Like this, perhaps?

db :=	{"AA-001":[1,2,3],"BB-002":[10,11,12]}
For crew, jobs in db
	For each, work_order in jobs
		MsgBox %crew%`t%work_order%


#3 neXt

neXt
  • Members
  • 533 posts

Posted 30 April 2012 - 04:24 PM

Yeah, this is awesome, thanks!
How do split array into lines, to make it visually more readable, ie:
this
db :=   {"AA-001":[1,2,3],"BB-002":[10,11,12]}
should be:
db :=   {"AA-001":[1,2,3],
		 "BB-002":[10,11,12]}


#4 neXt

neXt
  • Members
  • 533 posts

Posted 30 April 2012 - 04:58 PM

I can't seem to be able to assign values dynamically.
I'm going through a csv file line by line, here's what it looks like:
#include \\Cwidomain\Accounting\GENERAL\Scripts\lib\CSV.ahk


CSV_Load("open.csv", open_report, ",")

Loop % CSV_TotalRows(open_report) {
	i++
	
	work_order := CSV_ReadCell(open_report, i, 2)
	if work_order is not number
		continue
	
	account_code := CSV_ReadCell(open_report, i, 3)
	store_number := CSV_ReadCell(open_report, i, 4)
	service_description := CSV_ReadCell(open_report, i, 6)
	service_date := CSV_ReadCell(open_report, i, 7)
	crew_code := CSV_ReadCell(open_report, i, 9)
	crew_email := CSV_ReadCell(open_report, i, 45)
	
	store_address := CSV_ReadCell(open_report, i, 21)
	store_city := CSV_ReadCell(open_report, i, 22)
	store_state := CSV_ReadCell(open_report, i, 23)
	store_zip := CSV_ReadCell(open_report, i, 24)
	
	job_data := work_order ", " account_code ", " store_number ", " service_date ", " service_description
	db := {crew_code:[job_data]}
}

For crew, jobs in db
	For each, work_order in jobs
		MsgBox %crew%`t%work_order%
	
ExitApp

crew_code is treated as a literal for some reason. Also how do I push multiple work orders into the array dynamically? Right now it just stores one.

#5 jethrow

jethrow
  • Fellows
  • 2549 posts

Posted 30 April 2012 - 05:28 PM

db := {[color=#FF0000]([/color]crew_code[color=#FF0000])[/color]:[job_data]}



#6 neXt

neXt
  • Members
  • 533 posts

Posted 30 April 2012 - 05:57 PM

Thanks, that solved the variable part.
How do I append new items to db array? Right now it only shows the very last line from csv file.

#7 sinkfaze

sinkfaze
  • Moderators
  • 6087 posts

Posted 30 April 2012 - 07:04 PM

Is this what you mean?

#include \\Cwidomain\Accounting\GENERAL\Scripts\lib\CSV.ahk


c :=	{3:"Account Code",4:"Store Number",6:"Service Description",7:"Service Date",9:"Crew Code"
	 ,21:"Store Address",22:"Store City",23:"Store State",24:"Store Zip",45:"Crew Email"}	[color=#00BF00]; object to store CSV field names and row positions[/color]
jobs :=	[]	[color=#00BF00]; object to store data for each individual job[/color]
CSV_Load("open.csv", open_report, ",")
Loop %	CSV_TotalRows(open_report)
{
	i++
	work_order :=	CSV_ReadCell(open_report, i, 2)
	if	work_order is not number
		continue
	else	job_data :=	{"Work Order":work_order}
	For each, attribute in c	[color=#00BF00]; loop through and assign the field name and its data to the object[/color]
		job_data.Insert(attribute,CSV_ReadCell(open_report, i, each)
	jobs.Insert(job_data)
}
db :=	{(crew_code):jobs}	[color=#00BF00]; this assumes crew_code is a variable[/color]
For crew, jobs in db
{
	res :=	crew "`n"
	For type, data in jobs
		res .=	"`n" type ":`t" data
	MsgBox %	res
}
ExitApp