Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

Looping through a CSV File


  • Please log in to reply
13 replies to this topic
PaulW
  • Guests
  • Last active:
  • Joined: --
Hi
yet another question :)

Currently I'm looping through a TXT file using:

search := findit
	Loop, Read, test.txt
	{
   		StringSplit, item, A_LoopReadLine, `,, `r`n
   If ( item1 = search )
   	{
      BLAH BLAH BLAH
      Break
	}
	}

How would I do that using a CSV file ?

Thanks for your help :)

  • Guests
  • Last active:
  • Joined: --
Do what? CSV files are basically text files as far as AHK is concerned.

Leef_me
  • Moderators
  • 8510 posts
  • Last active: Sep 10 2015 05:50 AM
  • Joined: 08 Apr 2009

Do what? CSV files are basically text files as far as AHK is concerned.

1. Can't agree more.

2. Why not use "IfInString, var, SearchString" before the "StringSplit" ?
If what you are looking for is in the item it will be in the string before being split.

3. why not use

loop, item0
{
  If ( item%a_index% = search ) 
  {


Tuncay n-l-i mobile
  • Guests
  • Last active:
  • Joined: --
Do you know the csv parse loop?
FileRead, f, db.csv
Loop, Parse, f, 'n, 'r
{
    Loop, Parse, A_LoopReadLine, CSV
    {
        ...
    }
}
Be careful at this example. I cant type the escape character here.

Tuncay n-l-i mobile
  • Guests
  • Last active:
  • Joined: --
sorry i mean A_LoopField and not A_LoopReadLine

PaulW
  • Guests
  • Last active:
  • Joined: --
Thanks for the replies

I've looked at this again today, when not a tired !!
And have it working fine with csv.

Curious how I would use this

loop, item0
{
  If ( item%a_index% = search )
  {

with the example I posted ??

Thx

Leef_me
  • Moderators
  • 8510 posts
  • Last active: Sep 10 2015 05:50 AM
  • Joined: 08 Apr 2009

Curious how I would use this

StringSplit, item, A_LoopReadLine, `,, `r`n 
loop, item0
{
  If ( item%a_index% = search )
  {
with the example I posted ??

Your example uses stringsplit; if you need to do the same thing to each element in 'item' then you code can be more compact and yet readable.
<!-- m -->http://www.autohotke...StringSplit.htm<!-- m -->

The name of the array in which to store each substring extracted from InputVar. For example, if MyArray is specified,
:arrow: the command will put the number of substrings produced (0 if none) into MyArray0,
the first substring into MyArray1, the second into MyArray2, and so on.

btw, did you see my 2nd suggestion?
2. Why not use "IfInString, var, SearchString" before the "StringSplit" ?
If what you are looking for is in the item it will be in the string before being split.

answer4u
  • Guests
  • Last active:
  • Joined: --
Leef_me prolly meant
loop, [color=red]%[/color]item0[color=red]%[/color]

{

  If ( item%a_index% = search )

  {


PaulW
  • Guests
  • Last active:
  • Joined: --
Thanks for the replies.

btw, did you see my 2nd suggestion?
2. Why not use "IfInString, var, SearchString" before the "StringSplit" ?
If what you are looking for is in the item it will be in the string before being split.


Yes I saw your comment, and completely forgot to ask a follow-up about that !!

If each row in the csv contains numbers and names, would it cause a clash if I search for "1234" would I get 2 matches with the following:

1234,TEST,ABCD,008878
0123456789,AnotherTEST,EFGH,556647

I hope that makes sense !!

MasterFocus
  • Moderators
  • 4323 posts
  • Last active: Jan 28 2016 01:38 AM
  • Joined: 08 Apr 2009
Match = 1234

FileRead, FileContents, [color=red]myfile.extension[/color]

StringReplace, FileContents, FileContents, `r, , All

StringReplace, FileContents, FileContents, `n, `,, All

If Match in %FileContents%

  MsgBox Found!

Else

  MsgBox Not Found!

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Antonio França -- git.io -- github.com -- ahk4.net -- sites.google.com -- ahkscript.org

Member of the AHK community since 08/Apr/2009. Moderator since mid-2012.


PaulW
  • Guests
  • Last active:
  • Joined: --

Match = 1234
FileRead, FileContents, [color=red]myfile.extension[/color]
StringReplace, FileContents, FileContents, `r, , All
StringReplace, FileContents, FileContents, `n, `,, All
If Match in %FileContents%
  MsgBox Found!
Else
  MsgBox Not Found!


Sorry you've lost me a bit :(
Is this a replacement for my original code and would this be faster and use less resources ?

n-l-i-d
  • Guests
  • Last active:
  • Joined: --
These are two "main" options I guess.

; option 1, two loops

inputFile = test.csv
searchTerm = 1234

foundIt = false
Loop, Read, %inputFile% ; reads all lines one by one
{ 
   thisLine := A_LoopReadLine
   thisLineNr := A_Index
   Loop, Parse, thisLine, CSV ; goes thru all fields one by one 
   { 
      If (A_Index = 1) ; first field, else go to next line
      {
         If (A_LoopField = searchTerm) ; equal!
         {
             foundIt = true
             Break ; break out of line parse
         }
      }
   }
   If foundIt
      Break ; break out of file reading
}

MsgBox, , Found it!, Found "%searchTerm%" in first field of line %thisLineNr%`n`n%thisLine%


; option 2, a loop and an array

inputFile = test.csv
searchTerm = 1234

Loop, Read, %inputFile% ; reads all lines one by one
{
   thisLine := A_LoopReadLine
   thisLineNr := A_Index
   StringSplit, fieldArr, thisLine, `, ; get array of the fields
   If (fieldArr1 = searchTerm) ; first field
      Break ; break out of file reading
}

MsgBox, , Found it!, Found "%searchTerm%" in first field of line %thisLineNr%`n`n%thisLine%


HTH

MasterFocus
  • Moderators
  • 4323 posts
  • Last active: Jan 28 2016 01:38 AM
  • Joined: 08 Apr 2009

Is this a replacement for my original code

Have you tried it? I suppose it works.

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Antonio França -- git.io -- github.com -- ahk4.net -- sites.google.com -- ahkscript.org

Member of the AHK community since 08/Apr/2009. Moderator since mid-2012.


PaulW
  • Guests
  • Last active:
  • Joined: --
Thanks for this:
inputFile = test.csv
searchTerm = 1234

Loop, Read, %inputFile% ; reads all lines one by one
{
   thisLine := A_LoopReadLine
   thisLineNr := A_Index
   StringSplit, fieldArr, thisLine, `, ; get array of the fields
   If (fieldArr1 = searchTerm) ; first field
      Break ; break out of file reading
}

MsgBox, , Found it!, Found "%searchTerm%" in first field of line %thisLineNr%`n`n%thisLine%

Would this be faster, more memory efficient than :

search := 1234
	Loop, Read, numbermatch.csv
	{
   		StringSplit, item, A_LoopReadLine, `,, `r`n
    If ( item1 = search )
   	{
      var:=Notify(BLAH BLAH)
      Break
	}
	}
I combine that with another if statement, as below:
How should I write this to be more efficient ?
	search := word_array5
	Loop, Read, numbermatch.csv
	{
   		StringSplit, item, A_LoopReadLine, `,, `r`n
    If ( item1 = search )
   	{
      var:=Notify(BLAH BLAH)
      Break
	}
	}   
	
	if (item1 != search)
	{
	if word_array7 = %topa%
	{
    var:=Notify(BOO HO)
	} else {
    var:=Notify(YEH HAA)		
    }
	}

Thanks :)