Common Errors I have encountered with LoopRead

Put simple Tips and Tricks that are not entire Tutorials in this forum
l6908lay
Posts: 42
Joined: 09 Apr 2023, 05:16

Common Errors I have encountered with LoopRead

11 Apr 2023, 17:03

The Biggest error I have came accross While using LoopRead function is LineFeed returns. While reading a line, if I attempt to append that line to a new file the new linefeed return needs to be added

Code: Select all

FileAppend, % x, newfile.name, UTF-8
From the above code we can surmise that some "x" value will be appended to newfile.name in UTF-8 encoding. However, the linefeed returns aren't quite clear. I have found that when files are being read and placed in variables, so to is true for their linefeed returns. I have found that by clarifying a linefeed return for the entire file (such as `n) makes ordering to and from written files much easier. The only rub with this is when we attempt to LoopRead without declaring a parsing loop.

Code: Select all

Loop, read, newfile.name
{
	inside code to read the file lines
}
If you have changed the linefeed return to some new value a parsing loop is required. But even more apparent is if you extract information to put information back you will find that lines will have various different linefeed returns. Simalar to:
name - age - location... etc (`n)
name - age - location... etc (cr+lf)

Notice that the two lines are not exactly the same! This happens when reading a file and appending new to that file isn't done in uniformity. Simple workarounds would include formatting the end in uniform ways, String replace, even manual replace. But why do that when you can start it all out with exactly which linefeed return you want then just parse the loop by your known values.

Code: Select all

Loop, Read, newfile.name
{
	Loop, Parse, A_LoopReadLine, "`n" ;or anything you want really
	{
		inside code
	}
}
As you work with file reading loops and extract information and append information the errors that you will incur simply because the linefeed return from line to line isn't exactly the same going in and coming out will be frustrating to say the least. I usually will:

Code: Select all

FileRead, Contents, newfile.name, UTF-8
NewStr:=StrReplace(Contents, "`n`r", "`n") ; Or to whatever end you want!
This helps in uniformity of the file completely! of course the downfall is the need of the parsing loop for when you read the file again! It can be quite frustrating to go through all the work of reading and file appending large amounts of data just to see outcomes of irregularity
name - age - location... etc (`n)name - age - location... etc (cr+lf)
name - age - location... etc (`n)name - age - location... etc (cr+lf)
Simular to the above output.

I hope this helps in your future coding projects!

[Mod edit: Moved topic from 'Scripts and Functions'.]

Return to “Tips and Tricks (v1)”

Who is online

Users browsing this forum: No registered users and 24 guests