simple question on Loop Parse

Get help with using AutoHotkey and its commands and hotkeys
paik1002
Posts: 355
Joined: 28 Nov 2015, 02:45

simple question on Loop Parse

21 Oct 2018, 08:40

What are the differences between these loop parse functions?

1. Loop, Parse, InputVar, `r`n
2. Loop, Parse, InputVar, `n`r
3. Loop, Parse, InputVar, `r
4. Loop, Parse, InputVar, `n
swagfag
Posts: 3973
Joined: 11 Jan 2017, 17:59

Re: simple question on Loop Parse

21 Oct 2018, 11:07

Code: Select all

Loop Parse, InputVar, `r`n
Loop Parse, InputVar, `n`r
no difference, both will split on either a `r or `n

Code: Select all

Loop Parse, InputVar, `n
splits only on `n

Code: Select all

Loop Parse, InputVar, `r
splits only on `r
Banayat
Posts: 10
Joined: 08 Jul 2018, 14:29

Re: simple question on Loop Parse

21 Oct 2018, 11:09

Maybe the following can help you get a better idea:

Example:
Loop, parse, FileContents, `n, `r ; Specifying `n prior to `r allows both Windows and Unix files to be parsed.
{
MsgBox, 4, , Line number %A_Index% is %A_LoopField%.`n`nContinue?
IfMsgBox, No, break
}
Source: https://autohotkey.com/docs/commands/LoopParse.htm
`r = carriage return (sent the text cursor back to the beginning of the current text line)
`n = line feed (sent the cursor down one line at the same horizontal position on the page)
see girlgamer's post: https://autohotkey.com/board/topic/6589 ... ntry416609

Also, see: https://autohotkey.com/docs/misc/RegEx-QuickRef.htm
paik1002
Posts: 355
Joined: 28 Nov 2015, 02:45

Re: simple question on Loop Parse

24 Oct 2018, 02:35

Thank you.
So I guess Loop Parse, InputVar, `r`n will work for all.
swagfag
Posts: 3973
Joined: 11 Jan 2017, 17:59

Re: simple question on Loop Parse

24 Oct 2018, 07:08

i guess u could put it that way, if u acknowledge the caveat of having every other line be blank if the string has CRLF line endings
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: simple question on Loop Parse

24 Oct 2018, 09:18

Four ideas. Cheers.

Code: Select all

q:: ;handle line breaks
vText := "a,b,c,d,e"
vText1 := StrReplace(vText, ",", "`n")
vText2 := StrReplace(vText, ",", "`r")
vText3 := StrReplace(vText, ",", "`r`n")
vText4 := StrReplace(vText, ",", "`n`r")

;handles LF / CRLF / LFCR (but not CR):
vOutput := ""
Loop, 4
{
	vText := vText%A_Index%
	Loop, Parse, vText, `n, `r
		vOutput .= A_LoopField ","
	vOutput .= "`r`n"
}
MsgBox, % vOutput

;handles LF / CR / CRLF / LFCR:
vOutput := ""
Loop, 4
{
	vText := vText%A_Index%
	vText := StrReplace(vText, "`r`n", "`n")
	vText := StrReplace(vText, "`n`r", "`n")
	Loop, Parse, vText, `n`r
		vOutput .= A_LoopField ","
	vOutput .= "`r`n"
}
MsgBox, % vOutput

;handles LF / CR / CRLF / LFCR:
vOutput := ""
Loop, 4
{
	vText := vText%A_Index%
	vText := StrReplace(vText, "`r`n", "`n")
	vText := StrReplace(vText, "`n`r", "`n")
	vText := StrReplace(vText, "`r", "`n")
	Loop, Parse, vText, `n
		vOutput .= A_LoopField ","
	vOutput .= "`r`n"
}
MsgBox, % vOutput

;handles LF / CR / CRLF / LFCR:
vOutput := ""
Loop, 4
{
	vText := vText%A_Index%
	vText := RegExReplace(vText, "(?<!`n)`r(?!`n)", "`n")
	Loop, Parse, vText, `n, `r
		vOutput .= A_LoopField ","
	vOutput .= "`r`n"
}
MsgBox, % vOutput
theRe's No easy way out
To handle all 4 scenarios, you need StrReplace twice/thrice or 1 RegExReplace (note: using StrReplace multiple times can be faster than using RegExReplace).
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask For Help”

Who is online

Users browsing this forum: adamantine, aurelius, DanRim, Google [Bot], TAC109 and 37 guests