Reversing lines in a text variable Topic is solved

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

Reversing lines in a text variable

Post by paik1002 » 21 Sep 2018, 06:12

What is the fastest way to reverse the lines in a large-sized variable of text?

For example, if the text variable has:
apple
banana
orange
blackberry
coconut

Reversing the line order will yield:
coconut
blackberry
orange
banana
apple

Your kind help would be appreciated.
art
Posts: 18
Joined: 01 Jan 2014, 05:56

Re: Reversing lines in a text variable

Post by art » 21 Sep 2018, 06:51

Code: Select all

v=
(
apple
banana
orange
blackberry
coconut
)

s:=StringReverse(v, "`n")
MsgBox,4096,StringReverse, %s%
exitapp

;================================================================================
StringReverse(str, Separator="", OmitChars="") {
   Loop,Parse,% StrReplace(str,Separator,(BS:=chr(8))),%BS%,%OmitChars%
      i := A_LoopField (a_index=1 ? "" : BS i)
   Return StrReplace(i, BS, Separator)
}  ; A|B|CD --> CD|B|A
KnowNothing

Re: Reversing lines in a text variable

Post by KnowNothing » 21 Sep 2018, 06:59

not a nice solution but this is what i know, if your line has a number in front of each u can use the sort option

Code: Select all

RTEXT = 
(
apple
banana
orange
blackberry
coconut
)
FileAppend, %RTEXT%, FileOfItems.txt
loop, parse, RTEXT, `n
	NLINES := A_Index
loop, %NLINES%
{
FileReadLine, ToReverse, FileOfItems.txt, %NLINES%
NewReverseData .= ToReverse "`n"
NLINES--
}
msgbox, % NewReverseData
FileDelete, FileOfItems.txt
ExitApp
	
swagfag
Posts: 1217
Joined: 11 Jan 2017, 17:59

Re: Reversing lines in a text variable  Topic is solved

Post by swagfag » 21 Sep 2018, 07:17

Code: Select all

fruits =
(
apple
banana
orange
blackberry
coconut
)

Loop Parse, fruits, `n, `r
	result := A_LoopField "`n" result

MsgBox % result
paik1002
Posts: 320
Joined: 28 Nov 2015, 02:45

Re: Reversing lines in a text variable

Post by paik1002 » 22 Sep 2018, 02:45

Thanks to all for pitching in!
Guest

Re: Reversing lines in a text variable

Post by Guest » 28 Sep 2018, 01:51

Depending on other needs TF may be useful as it has https://github.com/hi5/TF#TF_ReverseLines function (may be slow on very large files and variables but posting here for reference)
Post Reply

Return to “Ask For Help”