CSV Search...How to return nothing if search is not in file Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ZipZip
Posts: 23
Joined: 08 Nov 2021, 13:21

CSV Search...How to return nothing if search is not in file

Post by ZipZip » 26 Jan 2022, 20:12

I have this below that searches a csv file .... 4 columns by 100 rows

Code: Select all

Loop, Read, List.csv
{
   StringSplit, item, A_LoopReadLine, `,, `r`n
   If ( item1 = 12345 )
	Break
}
	var1=%item2%
	var2=%item3%
	var3=%item4%

The results are great when the search number (ie 12345) is in the csv file, but when it isn't,... var1, 2 and 3 are just populated with the last row of the csv file.

How do I make it so if the search number isn't in the csv file the variables stay blank and a msgBox that says "search not found"?

Thank you for your help.

amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

Re: CSV Search...How to return nothing if search is not in file  Topic is solved

Post by amateur+ » 26 Jan 2022, 20:57

Code: Select all

found := false
Loop, Read, List.csv
{
	StringSplit, item, A_LoopReadLine, `,, `r`n
	If ( item1 = 12345 )
	{	found := true
		Break
	}
}
if !found 
{	item := "" ; optional
	MsgBox, Search not found
}
else {
	var1=%item2%
	var2=%item3%
	var3=%item4%
}
return
Have found any drawback in my code or approach? Please, point it out. /The moderator ordered to remove the rest of the signature, I had obeyed.
And I really apologize for our russian president. Being a citizen of an aggressor country is very shameful. Personally I tried to avoid this trying to defend elections from fraud being a member of the election commission of one of the precincts but only was subjected to a hooligan attack and right before the vote count was illegally escorted from the polling station and spent the night behind bars (in jail) in a result of illegal actions of corrupt policemen.

User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: CSV Search...How to return nothing if search is not in file

Post by mikeyww » 26 Jan 2022, 21:08

Another one:

Code: Select all

Line:
Loop, Read, %A_ScriptDir%\List.csv
 Loop, Parse, A_LoopReadLine, CSV
  Switch A_Index {
   Case 1 : If (A_LoopField = 12345)
             found := True
            Else Break, Line
   Default: v := A_Index - 1, var%v% := A_LoopField
  }
If found
     MsgBox, 64, Found, %var1%`n%var2%`n%var3%
Else MsgBox, 48, Error, Not found!

amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

Re: CSV Search...How to return nothing if search is not in file

Post by amateur+ » 26 Jan 2022, 21:58

@mikeyww, maybe your Line label should be a little bit below.
Have found any drawback in my code or approach? Please, point it out. /The moderator ordered to remove the rest of the signature, I had obeyed.
And I really apologize for our russian president. Being a citizen of an aggressor country is very shameful. Personally I tried to avoid this trying to defend elections from fraud being a member of the election commission of one of the precincts but only was subjected to a hooligan attack and right before the vote count was illegally escorted from the polling station and spent the night behind bars (in jail) in a result of illegal actions of corrupt policemen.

User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: CSV Search...How to return nothing if search is not in file

Post by mikeyww » 26 Jan 2022, 22:51

Oops, I think you're right. :) The following may work.

Code: Select all

Loop, Read, %A_ScriptDir%\List.csv
 Loop, Parse, A_LoopReadLine, CSV
  If (A_Index = 1)
   If (A_LoopField = 12345)
    found := True
   Else Break
  Else v := A_Index - 1, var%v% := A_LoopField
If found
     MsgBox, 64, Found, %var1%`n%var2%`n%var3%
Else MsgBox, 48, Error, Not found!
Some advantages to "CSV" approach here: can handle the following kind of standard CSV formatting.

a,b,c,d,e,f
12345,"a,x",b,c,d
x,1,2,3,4

image220126-2300-001.png
Output
image220126-2300-001.png (6.25 KiB) Viewed 432 times

amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

Re: CSV Search...How to return nothing if search is not in file

Post by amateur+ » 27 Jan 2022, 00:24

Yeah, these advantages seems to be huge!
Have found any drawback in my code or approach? Please, point it out. /The moderator ordered to remove the rest of the signature, I had obeyed.
And I really apologize for our russian president. Being a citizen of an aggressor country is very shameful. Personally I tried to avoid this trying to defend elections from fraud being a member of the election commission of one of the precincts but only was subjected to a hooligan attack and right before the vote count was illegally escorted from the polling station and spent the night behind bars (in jail) in a result of illegal actions of corrupt policemen.

ZipZip
Posts: 23
Joined: 08 Nov 2021, 13:21

Re: CSV Search...How to return nothing if search is not in file

Post by ZipZip » 27 Jan 2022, 05:58

Thank you for this....it's exactly what I was looking for. Much appreciated.


amateur+ wrote:
26 Jan 2022, 20:57

Code: Select all

found := false
Loop, Read, List.csv
{
	StringSplit, item, A_LoopReadLine, `,, `r`n
	If ( item1 = 12345 )
	{	found := true
		Break
	}
}
if !found 
{	item := "" ; optional
	MsgBox, Search not found
}
else {
	var1=%item2%
	var2=%item3%
	var3=%item4%
}
return

ZipZip
Posts: 23
Joined: 08 Nov 2021, 13:21

Re: CSV Search...How to return nothing if search is not in file

Post by ZipZip » 27 Jan 2022, 06:05

@mikeyww ... thank you for this answer. I will definitely explore it.

mikeyww wrote:
26 Jan 2022, 22:51
Oops, I think you're right. :) The following may work.

Code: Select all

Loop, Read, %A_ScriptDir%\List.csv
 Loop, Parse, A_LoopReadLine, CSV
  If (A_Index = 1)
   If (A_LoopField = 12345)
    found := True
   Else Break
  Else v := A_Index - 1, var%v% := A_LoopField
If found
     MsgBox, 64, Found, %var1%`n%var2%`n%var3%
Else MsgBox, 48, Error, Not found!
Some advantages to "CSV" approach here: can handle the following kind of standard CSV formatting.

a,b,c,d,e,f
12345,"a,x",b,c,d
x,1,2,3,4


image220126-2300-001.png

Post Reply

Return to “Ask for Help (v1)”