Fill associative array from CSV file?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
superpeter
Posts: 115
Joined: 18 Dec 2020, 05:17

Fill associative array from CSV file?

Post by superpeter » 04 Mar 2021, 02:20

I have the following CSV file (button_names.txt):

John,Agreeable
Sandy,Conscientious
Bob,Extraverted
Betty,Neurotic
Peter,Open-minded

I'd like to use it to fill an associative array where the key is the first word of each line, and the value is the second word. That is:

array := {John: Agreeable, Sandy: Conscientious, Bob: Extraverted}

I was only able to create the following. Thanks so much!

Code: Select all

Filereadline, line, C:\Users\super\Documents\button_names.txt, 1
loop, parse, line, CSV
	msgbox, %A_LoopField%
Rohwedder
Posts: 7610
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Fill associative array from CSV file?

Post by Rohwedder » 04 Mar 2021, 02:46

Hallo,
try:

Code: Select all

array := {}
Loop, Read, C:\Users\super\Documents\button_names.txt
{
	Names := StrSplit(A_LoopReadLine,",")
	array[Names.1] := Names.2
}
MsgBox,% array["John"]
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: Fill associative array from CSV file?

Post by Xtra » 04 Mar 2021, 02:55

CSV Example:

Code: Select all

button_names := {}
FileRead, myCsv, C:\Users\super\Documents\button_names.txt
Loop, Parse, myCsv, `n, `r
{
   line := A_LoopField
   Loop, Parse, line, CSV
   {
       switch A_Index
       {
           case 1:key := A_LoopField
           case 2:button_names[key] := A_LoopField
           Default:MsgBox, 4096, Error!, Parsing loop problem!
       }
   }
}
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Fill associative array from CSV file?

Post by BoBo » 04 Mar 2021, 12:04

Code: Select all

FileRead, content, C:\Users\super\Documents\button_names.txt

csv := StrSplit(content,"`n"), arr:={}
Loop % csv.Count()
	arr[StrSplit(csv[A_Index],",").1] := StrSplit(csv[A_Index],",").2

MsgBox % arr.Bob
Tested.
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: Fill associative array from CSV file?

Post by Xtra » 04 Mar 2021, 12:24

CSV format can be:

Code: Select all

John,Agreeable
Sandy,Conscientious
Bob,Extraverted
Betty,Neurotic
"Peter","Open-minded, maybe?"
superpeter
Posts: 115
Joined: 18 Dec 2020, 05:17

Re: Fill associative array from CSV file?

Post by superpeter » 04 Mar 2021, 13:24

Whoa! So many ways to do it, thanks so much @Xtra , @BoBo , @Rohwedder ! I teach at a college in Montreal and encourage students to learn AHK. The flexibility of this language will impress them. Thanks again guys!
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Fill associative array from CSV file?

Post by BoBo » 05 Mar 2021, 02:46

@Xtra AFAIK, quoted lines are used if chars like a comma are part of the value, but in this case a semi-colon would be used as the 'separator' (Excel-style). But I might be wrong.

Code: Select all

John,Agreeable
Sandy,Conscientious
Bob,Extraverted
Betty,Neurotic
"Peter";"Open-minded, maybe?"
User avatar
boiler
Posts: 16900
Joined: 21 Dec 2014, 02:44

Re: Fill associative array from CSV file?

Post by boiler » 05 Mar 2021, 04:28

@BoBo - Excel’s use of semicolons as delimiters is regional, such as in Europe. In the US and other regions (and as interpreted by AHK), comma delimiters are used. A field containing a comma would be surrounded by quotes and still delimited by a comma as @Xtra had shown when output by Excel in the US and other regions. This is how AHK’s loop parsing is expecting it as this excerpt from the Loop, Parse documentation states:
If this parameter is CSV, InputVar will be parsed in standard comma separated value format. Here is an example of a CSV line produced by MS Excel:

"first field",SecondField,"the word ""special"" is quoted literally",,"last field, has literal comma"
AHK’s Loop, Parse would handle the above while using StrSplit with a comma delimiter would not.
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Fill associative array from CSV file?

Post by BoBo » 05 Mar 2021, 05:08

@boiler - Thx for explaining that difference, that isn't necessarily obvious :thumbup:
Post Reply

Return to “Ask for Help (v1)”