Manipulating multiple lists Topic is solved

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

Manipulating multiple lists

25 Nov 2019, 16:49

This is more of a general question about how to approach a task than about a specific piece of code. My question is how to go about this or if there is a better way?

I have a CSV file. I am parsing the CSV file line by line. The first column contains the categories. So for example any lines, containing 'apple' as the first column would go into the apple array, any lines containing banana in the first column would go into the banana array and so on. Okay. The next thing I want is to select a line, string split it into it's own array, so I can manipulate it's elements. So it would all look like this:

1. parse CSV file. csv
2. Separate lines into lists. IE: Categories := [apple, banana, orange, etc.]
3. Separate individual lines into smaller lists. IE: Line 2 in the apple category would be, Apple_2 := ["apple", "red", "large", etc.]
4. Manipulate elements in selected line. IE: Apple_2.insertat(2, "green")

But then, once I make whatever changes to whichever lines, I want to append everything into a new CSV file. So I guess it would be like objects within an array. I am doing this as a sort of database. I agree it's probably not the best way to go about it. I also thought about doing an INI file instead, but I feel like ini files are more for settings than actual data. I have noticed the bigger they get, they become harder to read. Also I like the accessibility of spreadsheet or text file. There is also the possibility of using Com Objects with an excel spreadsheet but I am running libre office and that's a whole other animal. So any help or advise with this would be greatly appreciated.
guest3456
Posts: 3109
Joined: 09 Oct 2013, 10:31

Re: Manipulating multiple lists  Topic is solved

25 Nov 2019, 17:23

each time you parse a new line, and create an array for that line, just push that array onto a wholefile array. would that work?

Code: Select all

wholefile := []
; parse line2
Apple_2 := ["apple", "red", "large"]
; add this array to a bigger array
wholefile.push(Apple_2)
then at the end, you can traverse through the wholefile array and write each line individually

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

Re: Manipulating multiple lists

25 Nov 2019, 17:32

That is actually, a very good idea. In fact, I think I'll turn the entire CSV into an array. That way I can delete lines and add new lines as needed.
User avatar
tidbit
Posts: 1165
Joined: 29 Sep 2013, 17:15
Location: USA

Re: Manipulating multiple lists

25 Nov 2019, 17:47

made this a while ago

Code: Select all

/*
Name: 
Version 1.1 (Wednesday, December 02, 2015)
Created: (Sat February 16, 2013)
Author: tidbit
Credit:

Hotkeys:
	esc --- Quit

Description:
	load a csv to array or turn an array into csv.
*/
; Template script (you can customize this template by editing "ShellNew\Template.ahk" in your Windows folder)

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#SingleInstance, force
FileEncoding, UTF-8

; fileRead, csvdata, %A_ScriptDir%\hello.csv
csvdata=
(
1,"Do something","2,5,6",0,"aaa",20150416105540
2,"Do something","1,5",0,"bbb",20150416113128
2,"Do something","1,5",1,"ccc",20150416104640
3,"Do something","1,2,5,7",0,"ddd",20150416140828
)
o:=load(csvdata)

MsgBox % st_printArr(o)
exitapp
Esc::Exitapp


st_printArr(array, depth=5, indentLevel="")
{
	for k,v in Array
	{
		list.= indentLevel "[" k "]"
		if (IsObject(v) && depth>1)
			list.="`n" st_printArr(v, depth-1, indentLevel . "    ")
		Else
			list.=" => " v
		list.="`n"
	}
	return rtrim(list)
}

load(csv)
{
	arr:=[]
	loop, parse, csv, `n, `r
	{
		row:=a_index
		loop, parse, A_LoopField, CSV
		arr[row, a_index]:=A_LoopField
	}
	return arr
}


save(arr)
{
	for row, arr2 in arr
	{
		for k, val in arr2
		{
			if val is not number ; wrap non-numbers in quotes
				val:="""" val """"
			out.= val ","
		}
		out:=subStr(out, 1, -1) "`n" ; remove the unneeded last comma
	}
	; return out
	return rtrim(out, "`r`n")
}
rawr. fear me.
*poke*
Is it December 21, 2012 yet?
A_Perry_1984
Posts: 76
Joined: 07 Dec 2018, 12:08

Re: Manipulating multiple lists

25 Nov 2019, 18:47

This is great! Thanks Tidbit.

Return to “Ask For Help”

Who is online

Users browsing this forum: barbaque, colt, Google [Bot], kdservices, Ploaie and 174 guests