Parsing loop. Same line is repeated twice ?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Parsing loop. Same line is repeated twice ?

19 Jul 2018, 04:13

Hi guys,

Could someone please explain why this parsing loop repeats the second line in my file ?

My csv file looks like this:

Code: Select all

Nrpakbon;Cddeb;Naam klant;Naam2;Naam3;Straat+hnr;Postcode;Plaats;Naam land;aantal;eMail;PickId
485845;101920;Microsoft;Bill Gates;;Cash Cow street 12;1106 WK;Amsterdam;NL;1;[email protected];42955
<blank line>*
*
(the <blank line> part is actually a real blank line, not the text)

Code: Select all

if FileExist("testfile.csv")
{
	FileDelete, testfile.csv
	sleep, 2000
}
	
FileRead, MyFile, parceldata_RRUBINGH40047658.csv
IfInString, MyFile, 101920
{
	Loop, Parse, MyFile, `n, `r
	{
		StringSplit, data, A_LoopField, `;
			If (data1 = "") 
				continue
			else
			{
				FileAppend, %data1%`;%data2%`;%data4%`;;%data5%`;%data6%`;%data7%`;%data8%`;%data9%`;%data10%`;%data11%`;%data12%, testfile.csv
				If (data1 = "NrPakbon")
				{
					FileAppend, `n, testfile.csv
				}
			}
	}
}
It looks like the blank line is the problem. That's why I put If (data1 = "") hoping it would skip it, but it doesn't.

I end up with a file that looks like this:

Code: Select all

Nrpakbon;Cddeb;Naam klant;Naam2;Naam3;Straat+hnr;Postcode;Plaats;Naam land;aantal;eMail;PickId
485845;101920;Microsoft;Bill Gates;;Cash Cow street 12;1106 WK;Amsterdam;NL;1;[email protected];42955485845;101920;Microsoft;Bill Gates;;Cash Cow street 12;1106 WK;Amsterdam;NL;1;[email protected];42955
What am I doing wrong ?
just me
Posts: 9453
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Parsing loop. Same line is repeated twice ?

19 Jul 2018, 04:48

[b]StringSplit[/b] wrote:If the array elements already exist, the command will change the values of only the first N elements, where N is the number of substrings present in InputVar. Any elements beyond N that existed beforehand will be unchanged. Therefore, it is safest to use the zero element (MyArray0) to determine how many items were actually produced by the command.
That's why I prefer the StrSplit() function. It creates a complete new array each time it is called, so nothing can remain unchanged.
User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: Parsing loop. Same line is repeated twice ?

19 Jul 2018, 06:29

Thank you just me. Do you have a suggestion to change my code to make it work with StrSplit instead ?

I don't find the docs very clear on this topics.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Parsing loop. Same line is repeated twice ?

19 Jul 2018, 06:59

from the docs on the legacy version of StringSplit
For example, if MyArray is specified, 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.
in your script u check against data1 being empty. When StringSplit fails on the blank line, previously declared/defined pseudo-array vars(ie, data#) are not wiped as u were probably counting on when writing the piece of code.
So u either need to check against data0 != 0 or rewrite it w/o using deprecated commands, which i would personally advise you to do if thats a new script youre writing on the current version of ahk.

Code: Select all

Sample =
(
Nrpakbon;Cddeb;Naam klant;Naam2;Naam3;Straat+hnr;Postcode;Plaats;Naam land;aantal;eMail;PickId
485845;101920;Microsoft;Bill Gates;;Cash Cow street 12;1106 WK;Amsterdam;NL;1;[email protected];42955

Nrpakbon;Cddeb;Naam klant;Naam2;Naam3;Straat+hnr;Postcode;Plaats;Naam land;aantal;eMail;PickId
485845;101920;Microsoft;Bill Gates;;Cash Cow street 12;1106 WK;Amsterdam;NL;1;[email protected];42955

Nrpakbon;Cddeb;Naam klant;Naam2;Naam3;Straat+hnr;Postcode;Plaats;Naam land;aantal;eMail;PickId
485845;101920;Microsoft;Bill Gates;;Cash Cow street 12;1106 WK;Amsterdam;NL;1;[email protected];42955

)

for each, Line in StrSplit(Sample, "`n", "`r")
{
	if (Line != "")
	{
		Data := StrSplit(Line, ";")
		Result := Format("{};{};{};;{};{};{};{};{};{};{};{}", Data*)
		Result .= (Data[1] = "NrPakbon") ? "`n" : ""

		MsgBox % Result
	}
}


Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: gongnl, Google [Bot], RandomBoy, Rohwedder and 348 guests