A search in a CSV file

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Albireo
Posts: 1749
Joined: 16 Oct 2013, 13:53

A search in a CSV file

08 Jan 2019, 16:59

My desire is to get information from a csv-file, like this one.

Code: Select all

"100","Test One","0","0","1","1","Pass1",""
"101","Example","0","0","1","1","Passwd2",""
"999","Admin Name","50","99","1","9","x999x",""
....
( About 100 rows )
Only field1, field2 and field7 in the CSV-file are interesting.
(field1 and field2 are search fields, and field7 gives a result.)
- The search should handle both upper and lower case letters.
- field1 is always numbers, field2 is always characters (and possibly a dash)
- field1 is an unique field
- - - - - - - - - - - - - - - - - - - - - - - - - - - -

Example 1
If "101" is written in the input field, on the input window.
The answer1 = Example (field2) and field7 = Passwd2


Example 2
If "test one" is written in the input field on the input window.
The answer1 = 100 (field1) and field7 = Pass1


Example2a (Maybe it's possible?)
Is there only one row (field2) that begins with "test", does the program suggest "Test One"?
(and if it is not difficult to implement)
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: A search in a CSV file

08 Jan 2019, 17:37

This a start:

Code: Select all

; get info from file
FileRead, OutputVar, Test.csv ; csv-file

; put info in array
CSV := {}
for Index, Line in StrSplit(OutputVar, "`n", "`r")
   CSV.Push(StrSplit(Line, ","))

; example of get specific item and field
MsgBox % CSV.2.7 ; item 2, field 7

; example of looping through all items and showing specific fields
for Index, Fields in CSV
   MsgBox % "Item #" Index "`nField 1:`t" Fields.1 "`nField 2:`t" Fields.2 "`nField 3:`t" Fields.7
Not totally clear on what you wanted in the bottom part of your post but this shows how to read a csv-file into an array that allows easy access to the information.

You can then check, compare, search, format, display, etc. that information however you like.

Thanks,
Larry
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
DRocks
Posts: 565
Joined: 08 May 2018, 10:20

Re: A search in a CSV file

08 Jan 2019, 21:35

MsgBox % CSV.2.7 ; item 2, field 7
I cant understand how you made a 2 step object dotation with the two StrSplits.

I always thought an object was limited to only 1 dot like this:
Csv.2
Csv.3
Etc.

Can you please add more verbal explanations to the First For Loop?

Thanks in advance :)
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: A search in a CSV file

09 Jan 2019, 13:36

DRocks wrote:
08 Jan 2019, 21:35
MsgBox % CSV.2.7 ; item 2, field 7
I cant understand how you made a 2 step object dotation with the two StrSplits.

I always thought an object was limited to only 1 dot like this:
Csv.2
Csv.3
Etc.

Can you please add more verbal explanations to the First For Loop?

Thanks in advance :)

Here is the loop that produces the CVS array"

Code: Select all

for Index, Line in StrSplit(OutputVar, "`n", "`r")
   CSV.Push(StrSplit(Line, ","))

To break it down here is the first StrSplit.
StrSplit(OutputVar, "`n", "`r") produces an array that looks like this:

Code: Select all

[1] => "100","Test One","0","0","1","1","Pass1",""
[2] => "101","Example","0","0","1","1","Passwd2",""
[3] => "999","Admin Name","50","99","1","9","x999x",""

The StrSplit(Line, ",") on the first element above produces this:

Code: Select all

[1] => "100"
[2] => "Test One"
[3] => "0"
[4] => "0"
[5] => "1"
[6] => "1"
[7] => "Pass1"
[8] => ""

This is then Pushed into the CSV array to produce this:

Code: Select all

[1]
    [1] => "100"
    [2] => "Test One"
    [3] => "0"
    [4] => "0"
    [5] => "1"
    [6] => "1"
    [7] => "Pass1"
    [8] => ""

Repeat for each element:

Code: Select all

[1]
    [1] => "100"
    [2] => "Test One"
    [3] => "0"
    [4] => "0"
    [5] => "1"
    [6] => "1"
    [7] => "Pass1"
    [8] => ""

[2]
    [1] => "101"
    [2] => "Example"
    [3] => "0"
    [4] => "0"
    [5] => "1"
    [6] => "1"
    [7] => "Passwd2"
    [8] => ""

[3]
    [1] => "999"
    [2] => "Admin Name"
    [3] => "50"
    [4] => "99"
    [5] => "1"
    [6] => "9"
    [7] => "x999x"
    [8] => ""

As for dot notation, you can have an many dots as you like. The CSV.2.7 is just shorthand for CSV[2,7]. I am sure there is a technical limit to the number of dimensions an array can have but it is probably very large.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
DRocks
Posts: 565
Joined: 08 May 2018, 10:20

Re: A search in a CSV file

09 Jan 2019, 17:09

FanaticGuru wrote:
09 Jan 2019, 13:36
To break it down here is the first StrSplit.
FG
Thank you very much!! awesome. I'll study that

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Anput, mcd, mikeyww, Nerafius and 128 guests