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.
¯¯¯¯¯¯¯¯¯¯¯¯¯
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.
¯¯¯¯¯¯¯¯¯¯
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
}