LineDelete() - Delete Line Of Text From A Variable / No Loop

Post your working scripts, libraries and tools
User avatar
Cuadrix
Posts: 204
Joined: 07 May 2017, 08:26

LineDelete() - Delete Line Of Text From A Variable / No Loop

01 Apr 2018, 20:43

LineDelete()

Deletes a specific line or a range of lines from a variable containing one or more lines of text.

Code: Select all

OutputVar := LineDelete(InVar, Pos [, Range, Options := "B", DumpVar]) ;Parameters inside [] are optional.
Parameters
¯¯¯¯¯¯¯¯¯¯¯¯¯
InVar
| The variable whose lines will be deleted.

Pos
| The line which will be deleted.

Range
| If this parameter is set, Pos will act as the first line and Range as the last line. All lines in-between and including Pos and Range will be deleted.

Options
| If Range has been set, using "B" in Options will make the operation prevent Pos and Range from being deleted, and only delete what's in-between them.

DumpVar
| Specify a variable in which to store the deleted lines from the operation.


Return Value
¯¯¯¯¯¯¯¯¯¯¯¯¯¯

This function returns a version of InVar whose contents have been altered by the operation to OutputVar. If no alterations are needed, InVar is returned unaltered.

Examples
¯¯¯¯¯¯¯¯¯¯¯

Code: Select all

Var =
(
One
Two
Three
Four
Five
)
Output := LineDelete(Var, 2) ; Deletes line 2.
MsgBox, % Output ; Returns variable's contents without line 2.

Code: Select all

Var =
(
One
Two
Three
Four
Five
)
Output := LineDelete(Var, -1) ; Deletes the last line. Similarly, inputting -2 will delete the second last line and so on.
MsgBox, % Output ; Returns variable's contents without the last line.

Code: Select all

Var =
(
One
Two
Three
Four
Five
)
Output := LineDelete(Var, 2, 5, "B") ; Deletes lines in-between 2 and 5. If "B" is omitted, lines 2 and 5 will also be deleted.
MsgBox, % Output ; Returns variable's contents without lines 3 and 4.

Code: Select all

Var =
(
One
Two
Three
Four
Five
)
Output := LineDelete(Var, 2+1, -1) ; Deletes all lines after line 2. Setting Pos to 3+1 instead will delete all lines after 3 and so on.
MsgBox, % Output ; Returns variable's contents without lines 3, 4 and 5.
How to use?
¯¯¯¯¯¯¯¯¯¯

Method 1: #Include

Download LineDelete.ahk, place it in the script directory and include it in the script by placing #Include LineDelete.ahk on top of the script in which LineDelete function is going to be used in. This must be done every time a user wishes to use the LineDelete function.

Method 2: Place In-Script

Copy the code of LineDelete function, and place it somewhere in the script. To use the function again in a different script, it must again be placed somewhere in the new script.

Method 3: The Lib Folder

Place LineDelete.ahk to Autohotkey's standard library located at C:\Program Files\AutoHotkey\Lib for 64-bit or C:\Program Files (x86)\AutoHotkey\Lib for 32-bit. If the folder doesn't exist, just create it. Now the LineDelete function can be used in all your scripts without the need to explicitly add it to your script every time you create a new .ahk file.


LineDelete()
¯¯¯¯¯¯¯¯¯¯¯¯¯

Code: Select all

LineDelete(Str, Pos, EndPos := "", Opts := "", ByRef Match := ""){
	If (Pos = "" || Pos = 0 || Pos * 1 = "")
		Return Str
	LF := (inStr(Str, "`r`n")) ? ("`r`n")
		: (inStr(Str, "`n")) ? ("`n") : ("")
	L := StrLen(LF)
	Str := LF . StrReplace(Str, LF, LF, NumLines) . LF
	NumLines := (SubStr(Str, 0) != LF) ? (++NumLines) : (++NumLines)
	Pos := (Pos < 0 && Pos >= -NumLines) ? (Pos + NumLines + 1)
		 : (Pos < -NumLines || Pos > NumLines) ? ("") : (Pos)
	EndPos := (Pos = "" || EndPos * 1 = "" || EndPos = 0
			   || EndPos < -NumLines || EndPos > NumLines) ? ("")
			: (EndPos < 0) ? (EndPos + NumLines + 1) : (EndPos)
	(EndPos != "" && EndPos < Pos)
			 ? (Tmp := Pos, Pos := EndPos, EndPos := Tmp, Tmp := "")
	Opts := ((Opts != "B" && Opts != "") || (Pos = EndPos && Opts = "B")
			 || (EndPos = "" && Opts = "B")) ? ("") : (Opts)
	StartMatch := (Opts = "") ? (InStr(Str, LF,,, Pos) + L)
				: (InStr(Str, LF,,, Pos + 1) + L)
	EndMatch := (EndPos = "" && Opts = "") ? (InStr(Str, LF,,, Pos + 1))
			  : (EndPos != "" && Opts != "") ? (InStr(Str, LF,,, EndPos))
			  : (InStr(Str, LF,,, EndPos + 1))
	Match := ((EndPos - Pos = 1 && Opts = "B")
			  || Pos = "") ? ("")
		   : (SubStr(Str, StartMatch, EndMatch - StartMatch))
	Str := SubStr(SubStr(Str, 1, StartMatch - 1)
	     . SubStr(Str, EndMatch + L), L + 1, -L)
	Return Str
}
Attachments
LineDelete.ahk
The .ahk file containing LineDelete() function
(1.33 KiB) Downloaded 14 times
Last edited by Cuadrix on 19 May 2019, 19:09, edited 25 times in total.
----
ozzii
Posts: 327
Joined: 30 Oct 2013, 06:04

Re: LineDelete()

02 Apr 2018, 02:39

Thanks, this can be handy
User avatar
Cuadrix
Posts: 204
Joined: 07 May 2017, 08:26

Re: LineDelete()

02 Apr 2018, 21:14

I edited the code a little;
• Now it looks a titty bitty neater.
• The function will return InVar unaltered if a non-existent option is used.
----
User avatar
Cuadrix
Posts: 204
Joined: 07 May 2017, 08:26

Re: LineDelete() - Delete lines from variables

07 Apr 2018, 08:09

Updated;
• The script is now 1.8 times faster.
----
User avatar
Cuadrix
Posts: 204
Joined: 07 May 2017, 08:26

Re: LineDelete() - Delete lines from variables

10 Apr 2018, 18:32

Updated;
• Redesigned script code.
• Replaced Array syntax to pure ...Str...()
• The script is now immensely faster.
• Added the DumpVar parameter for storing deleted lines.
----
DRocks
Posts: 525
Joined: 08 May 2018, 10:20

Re: LineDelete() - Delete Lines Of Text From Variables / No Loop

22 Oct 2018, 05:36

Nice one thank you for sharing and detailed introduction.
May I ask you in what context you are using it?

I am thinking about maybe working with RAM rather than iniwrite/read using variables in some cases is that a typical use ?
User avatar
Frosti
Posts: 265
Joined: 27 Oct 2017, 14:30
GitHub: Ixiko

Re: LineDelete() - Delete Lines Of Text From Variables / No Loop

22 Oct 2018, 08:30

I tried your function and it works well. I'am using it to show 20 WinHookEvents in a ToolTip. I tried this before by myself, but it won't work so smart like yours. I append a picture to# show what is possible with your function.

Image
User avatar
Cuadrix
Posts: 204
Joined: 07 May 2017, 08:26

Re: LineDelete() - Delete Lines Of Text From Variables / No Loop

27 Oct 2018, 10:23

@DRocks
It depends. In my opinion, operations such as deleting lines should be done in the memory as long as you have RAM to spare, which shouldn't be a problem on modern machines today.
You could do it with IniWrite/Read too, but that just means that you are making temporary files and discarding them after the operation is finished, which I think is unnecessary and a bit inelegant.

@Frosti
That's one hell of a ToolTip. Glad it was of some use
----

Return to “Scripts and Functions”

Who is online

Users browsing this forum: No registered users and 61 guests