text/list/table functions
Posted: 18 Jan 2017, 02:19
I've made working attempts at, but am looking for the best ways to achieve:
- list comparison: items unique to list A / items unique to list B / items present in both [EDIT: see post lower down]
- table lookup: multiple values against a table/ini file [EDIT: just use an AHK array]
- table lookup: one value against a list (e.g. a spell list, do you use 26^2=676 variables to speed it up?) [EDIT: just use an AHK array][Note: for creating AHK arrays, it is faster to add keys in alphabetical order.]
- list remove duplicates: remove duplicates, maintain order [EDIT: see post lower down]
- list sort: sort list A based on list B, items in both should be in list B's order, the remaining items in list A should be in their original order [EDIT: see post lower down (list comparison)]
- list sort: start with paths/registry keys in their correct order, randomise, and restore original order [EDIT: will post code soon]
- text split: get start/end positions of nth line (see below) [EDIT: see post lower down]
- list line frequency: get frequency for each item, maintain order [EDIT: see post lower down]
- (each function would need case sensitive/case insensitive as an option)
get line: I made an attempt at this function below:
get nth/nth-to-last line of between a and b characters in length
(returning positions c and d, where the line starts after char c and ends before char d)
(note: the line could be blank, you can't use: starts with char c and ends with char d)
I'm thinking of methods including: multiple variables, arrays(?),
RegEx(?), add column - sort - remove column - sort back.
I've actually written all of these functions but they're untidy,
and am curious about any better methods, before I consider sharing.
These functions are the worst-looking/hardest-to-understand functions in my libraries.
(Something that became clear as I split my lib file into smaller files.)
I had hoped that maybe arrays might be a gamechanger, or some other method,
and wondered if anybody had any good ideas for these.
I was partly prompted by this recent question on csvs, that needs
the last non-blank line in a text file, of course you can use a parsing loop,
but what about more efficient ways:
(plus Excel can always corrupt/do weird things to the data,
because of assumptions it makes that can't be turned off)
Overwrite last row from one workbook to another - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=26949
[improved version of this in a post below]
- list comparison: items unique to list A / items unique to list B / items present in both [EDIT: see post lower down]
- table lookup: multiple values against a table/ini file [EDIT: just use an AHK array]
- table lookup: one value against a list (e.g. a spell list, do you use 26^2=676 variables to speed it up?) [EDIT: just use an AHK array][Note: for creating AHK arrays, it is faster to add keys in alphabetical order.]
- list remove duplicates: remove duplicates, maintain order [EDIT: see post lower down]
- list sort: sort list A based on list B, items in both should be in list B's order, the remaining items in list A should be in their original order [EDIT: see post lower down (list comparison)]
- list sort: start with paths/registry keys in their correct order, randomise, and restore original order [EDIT: will post code soon]
- text split: get start/end positions of nth line (see below) [EDIT: see post lower down]
- list line frequency: get frequency for each item, maintain order [EDIT: see post lower down]
- (each function would need case sensitive/case insensitive as an option)
get line: I made an attempt at this function below:
get nth/nth-to-last line of between a and b characters in length
(returning positions c and d, where the line starts after char c and ends before char d)
(note: the line could be blank, you can't use: starts with char c and ends with char d)
I'm thinking of methods including: multiple variables, arrays(?),
RegEx(?), add column - sort - remove column - sort back.
I've actually written all of these functions but they're untidy,
and am curious about any better methods, before I consider sharing.
These functions are the worst-looking/hardest-to-understand functions in my libraries.
(Something that became clear as I split my lib file into smaller files.)
I had hoped that maybe arrays might be a gamechanger, or some other method,
and wondered if anybody had any good ideas for these.
I was partly prompted by this recent question on csvs, that needs
the last non-blank line in a text file, of course you can use a parsing loop,
but what about more efficient ways:
(plus Excel can always corrupt/do weird things to the data,
because of assumptions it makes that can't be turned off)
Overwrite last row from one workbook to another - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=26949
[improved version of this in a post below]
Code: Select all
;==================================================
;this function is in beta stage
;if vNum is negative, the nth-to-last line positions are retrieved
;vRange1 and vRange2 allow you get the nth line with minimum/maximum length
;assumes CRLF line breaks
JEE_StrGetLinePos(ByRef vText, vNum, ByRef vPos1, ByRef vPos2, vRange1=0, vRange2="")
{
if (SubStr(vNum, 1, 1) = "-")
vIsNeg := 1, vNum := SubStr(vNum, 2)
if vNum is not digit
Return ""
if (vNum = "") OR (vNum = 0)
Return ""
if vIsNeg
vNum := -vNum
vCount := 0
vLen := StrLen(vText)
;==============================
;GET NTH LINE OF ANY LENGTH
if (vNum > 0) AND (vRange1=0) AND (vRange2="")
{
vPos2 := InStr(vText "`r`n", "`n", 0, 1, vNum)
if !vPos2
Return 0, vPos1 := 0, vPos2 := 0
vPos1 := InStr(vText "`r`n", "`n", 0, vPos2-vLen-3)
Return 1, vPos1, vPos2 := vPos2-2
}
;==============================
;GET NTH LINE OF SPECIFIED LENGTH
if (vNum > 0)
{
vPos2 := 0
Loop
{
vPos1 := vPos2+1
vPos2 := InStr(vText "`r`n", "`n", 0, vPos1+1)
if !vPos2
Return 0, vPos1 := 0, vPos2 := 0
vDiff := vPos2-vPos1-1
if (vDiff >= vRange1) AND ((vRange2 = "") OR (vDiff <= vRange2))
vCount++
if (vCount = vNum)
Return 1, vPos1 := vPos1-1, vPos2 := vPos2-2
}
}
;==============================
;GET NTH-TO-LAST LINE OF ANY LENGTH
if (vNum < 0) AND (vRange1=0) AND (vRange2="")
{
vPos1 := InStr("`r`n" vText, "`n", 0, 0, -vNum)
if !vPos1
Return 0, vPos1 := 0, vPos2 := 0
vPos2 := InStr("`r`n" vText "`r`n", "`n", 0, vPos1+1)
Return 1, vPos1 := vPos1-2, vPos2 := vPos2-4
}
;==============================
;GET NTH-TO-LAST LINE OF SPECIFIED LENGTH
if (vNum < 0)
{
vPos1 := vLen+4
Loop
{
vPos2 := vPos1-1
vPos1 := InStr("`r`n" vText "`r`n", "`n", 0, vPos2-vLen-4)
if !vPos1
Return 0, vPos1 := 0, vPos2 := 0
vDiff := vPos2-vPos1
if (vDiff >= vRange1) AND ((vRange2 = "") OR (vDiff <= vRange2))
vCount++
if (vCount = -vNum)
Return 1, vPos1 := vPos1-2, vPos2 := vPos2-3
}
}
;==============================
Return
}
;==================================================