Delineate StrSplit() by new line?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Morf
Posts: 4
Joined: 20 Oct 2017, 07:00

Delineate StrSplit() by new line?

20 Oct 2017, 18:32

So I have a gui that has a text box. In this text box, I expect my users to copy and paste data. The data will usually have multiple lines such as:

Code: Select all

This-Is-Line-One
This-Is-Line-Two
And-This-Is-Line-three
I am trying to get to where I can eventually use an array to call on each line at a time. To do this, I am attempting to use StrSplit. I have been able to get it to work when I use a "," but for some reason I can't get it to work with a new line/return.

Here is my code below, please assist.

This one doesn't work and is what I want to work using new line or return

Code: Select all

	GuiControlGet, codesList
	list := StrSplit(codesList, "'n")
	Answer := list[2] 
	listLength := list.MaxIndex()
	MsgBox, The second item is %Answer%
This works just fine using a comma, but that isn't what I want to use to separate based on the items we will be copy/pasting to the field.

Code: Select all

	GuiControlGet, codesList
	list := StrSplit(codesList, ",")
	Answer := list[2] 
	listLength := list.MaxIndex()
	MsgBox, The second item is %Answer%
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Delineate StrSplit() by new line?

20 Oct 2017, 18:39

You need to use the backtick character, not an apostrophe, for `n. Backtick, which is a rare character, is a better choice for an escape character, than apostrophe, which is quite common.
#EscapeChar (and explanation of escape sequences)
https://autohotkey.com/docs/commands/_EscapeChar.htm

The text is probably CRLF-delimited not LF-delimited, so you'd want to use `r`n rather than `n. Most likely `n would work, but you'd get unexpected trailing `r characters in all items (except the last item). Cheers.

Code: Select all

q::
;LF-delimited
vText := "abc`ndef`nghi"
oArray := StrSplit(vText, "`n")
MsgBox, % oArray.2
MsgBox, % oArray.Length()

;CRLF-delimited
vText := "abc`r`ndef`r`nghi"
oArray := StrSplit(vText, "`r`n")
MsgBox, % oArray.2
MsgBox, % oArray.Length()

;multiple delimiters e.g. \/
vText := "abc\def/ghi"
oArray := StrSplit(vText, ["\","/"])
MsgBox, % oArray.2
MsgBox, % oArray.Length()
return
Last edited by jeeswg on 20 Oct 2017, 18:43, edited 1 time in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Morf
Posts: 4
Joined: 20 Oct 2017, 07:00

Re: Delineate StrSplit() by new line?

20 Oct 2017, 18:43

OMG, i can't believe I kept doing that over and over and over. I new it just slipped my mind. Thanks!
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Delineate StrSplit() by new line?

20 Oct 2017, 18:53

Hehe classic. I hardly ever press `, I have various hotstrings. I use ` to switch between windows, and ctrl+` to type `.

I often use the PC from a TV, and don't have it zoomed in enough to tell ` from ', but I noticed people getting mixed up with ` and ' before so this time I zoomed in with my magnifier script to double-check if that was the issue.

Code: Select all

#IfWinActive, ahk_class Notepad
;AutoHotkey characters
:*:r2::"``r``n"
:*:r3::``r``n
:*:r4::``r``n
:*:n2::"``n"
:*:n3::``n
:*:n4::``n
:*:t2::"``t"
:*:t3::``t
:*:t4::``t
:*:s2::" " `
#IfWinActive

#IfWinActive, ahk_class #32770
;AutoHotkey characters
:*:r2::"``r``n"
:*:r3::``r``n
:*:r4::``r``n
:*:n2::"``n"
:*:n3::``n
:*:n4::``n
:*:t2::"``t"
:*:t3::``t
:*:t4::``t
:*:s2::" " `
#IfWinActive
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: Delineate StrSplit() by new line?

20 Oct 2017, 18:55

Instead of using oArray := StrSplit(vText, "`n") or oArray := StrSplit(vText, "`r`n") you could use oArray := StrSplit(vText, "`n", "`r"). It will work for both LF and CRLF.
Morf
Posts: 4
Joined: 20 Oct 2017, 07:00

Re: Delineate StrSplit() by new line?

20 Oct 2017, 19:28

teadrinker wrote:Instead of using oArray := StrSplit(vText, "`n") or oArray := StrSplit(vText, "`r`n") you could use oArray := StrSplit(vText, "`n", "`r"). It will work for both LF and CRLF.
I thought if you added the second comma it would omit the second item (thus omiting "`r")
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: Delineate StrSplit() by new line?

20 Oct 2017, 19:45

I'm not sure, which comma do you mean?
User avatar
boiler
Posts: 16932
Joined: 21 Dec 2014, 02:44

Re: Delineate StrSplit() by new line?

20 Oct 2017, 23:55

It will omit `r, so it will work for `n or `n`r because if it has both, it will split based on `n and omit the `r.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: doodles333 and 385 guests