Note: these functions have been tested but not exhaustively.
I may introduce versions of these functions that use one of 4 custom classes: case sensitive/insensitive, alphabetical/date creation order. I.e. custom types of array similar to the standard AHK arrays and the Scripting.Dictionary object.
- AHK arrays are case insensitive and alphabetical order.
- The Scripting.Dictionary object is case sensitive and date creation (date added) order.
- AHK arrays can handle string and numeric keys, e.g. it can have a numeric '1' key, and a string '1' key. The Scripting.Dictionary cannot handle numeric keys.
- One way to maintain the order of keys, is for the main array, with key K, to have an auxiliary array, with key N (number) and value K. You can then loop through the auxiliary array to retrieve the keys in order.
to create 'key 1, value 1', where value 1 is the name of a key in another array.
Code: Select all
;1 list - frequency count
;1 list - remove duplicates
;2 lists - compare (unique to A/unique to B/present in both)
;list of functions:
;JEE_StrFreqCIA(ByRef vText)
;JEE_StrFreqCID(ByRef vText)
;JEE_StrFreqCID2(ByRef vText)
;JEE_StrFreqCSA(ByRef vText)
;JEE_StrFreqCSD(ByRef vText)
;JEE_StrRemoveDupsCIA(ByRef vText)
;JEE_StrRemoveDupsCID(ByRef vText)
;JEE_StrRemoveDupsCID2(ByRef vText)
;JEE_StrRemoveDupsCSA(ByRef vText)
;JEE_StrRemoveDupsCSD(ByRef vText)
;JEE_StrListCompareCIA(ByRef vText1, ByRef vText2, ByRef vOutputAB, ByRef vOutputA, ByRef vOutputB)
;JEE_StrListCompareCID(ByRef vText1, ByRef vText2, ByRef vOutputAB, ByRef vOutputA, ByRef vOutputB)
;JEE_StrListCompareCID2(ByRef vText1, ByRef vText2, ByRef vOutputAB, ByRef vOutputA, ByRef vOutputB)
;JEE_StrListCompareCSA(ByRef vText1, ByRef vText2, ByRef vOutputAB, ByRef vOutputA, ByRef vOutputB)
;JEE_StrListCompareCSD(ByRef vText1, ByRef vText2, ByRef vOutputAB, ByRef vOutputA, ByRef vOutputB)
;some functions require:
;JEE_SortCasIns
;==================================================
;ARRAY TYPE 1: 'CIA', case insensitive, alphabetical order
;ARRAY TYPE 2: 'CID', case insensitive, date creation order
;ARRAY TYPE 3: 'CSA', case sensitive, alphabetical order
;ARRAY TYPE 4: 'CSD', case sensitive, date creation order
;==================================================
;list line frequency ('CIA', case insensitive, alphabetical order)
JEE_StrFreqCIA(ByRef vText)
{
oArray := {}
StrReplace(vText, "`n", "", vCount)
oArray.SetCapacity(vCount+1)
Loop, Parse, vText, `n, `r
if oArray.HasKey("" A_LoopField)
oArray["" A_LoopField]++
else
oArray["" A_LoopField] := 1
vOutput := ""
VarSetCapacity(vOutput, StrLen(vText)*2*2)
for vKey, vValue in oArray
vOutput .= vValue "`t" vKey "`r`n"
oArray := ""
return vOutput
}
;==================================================
;list line frequency ('CID', case insensitive, date creation order)
JEE_StrFreqCID(ByRef vText)
{
oArray := ComObjCreate("Scripting.Dictionary"), oArray2 := {}
Loop, Parse, vText, `n, `r
if oArray2.HasKey("" A_LoopField)
oArray.item(oArray2["" A_LoopField])++
else
oArray.item("" A_LoopField) := 1, oArray2["" A_LoopField] := "" A_LoopField
vOutput := ""
VarSetCapacity(vOutput, StrLen(vText)*2*2)
for vKey, vValue in oArray
vOutput .= oArray.item(vKey) "`t" vKey "`r`n"
oArray := oArray2 := ""
return vOutput
}
;==================================================
;list line frequency ('CID', case insensitive, date creation order)
;slower alternative to JEE_StrFreqCID
JEE_StrFreqCID2(ByRef vText)
{
oArray := {}, oArray2 := {}
StrReplace(vText, "`n", "", vCount)
oArray.SetCapacity(vCount+1)
oArray2.SetCapacity(vCount+1)
Loop, Parse, vText, `n, `r
if oArray.HasKey("" A_LoopField)
oArray["" A_LoopField]++
else
oArray["" A_LoopField] := 1, oArray2.Push("" A_LoopField)
vOutput := ""
VarSetCapacity(vOutput, StrLen(vText)*2*2)
for _, vValue in oArray2
vOutput .= oArray["" vValue] "`t" vValue "`r`n"
oArray := oArray2 := ""
return vOutput
}
;==================================================
;list line frequency ('CSA', case sensitive, alphabetical order)
JEE_StrFreqCSA(ByRef vText, vCSWithinCI=0)
{
oArray := ComObjCreate("Scripting.Dictionary")
vText2 := StrReplace(vText, "`r`n", "`n")
Sort, vText2, CS
;e.g. case sensitive: 'A,B,C,a,b,c'
;e.g. 'CS within CI': 'A,a,B,b,C,c'
if vCSWithinCI
Sort, vText2, F JEE_SortCasIns
Loop, Parse, vText2, `n, `r
oArray.item("" A_LoopField) := 0
Loop, Parse, vText, `n, `r
oArray.item("" A_LoopField)++
vOutput := ""
VarSetCapacity(vOutput, StrLen(vText)*2*2)
for vKey in oArray
vOutput .= oArray.item(vKey) "`t" vKey "`r`n"
oArray := ""
return vOutput
}
;==================================================
;list line frequency ('CSD', case sensitive, date creation order)
JEE_StrFreqCSD(ByRef vText)
{
oArray := ComObjCreate("Scripting.Dictionary")
Loop, Parse, vText, `n, `r
if !(oArray.item("" A_LoopField) = "")
oArray.item("" A_LoopField)++
else
oArray.item("" A_LoopField) := 1
vOutput := ""
VarSetCapacity(vOutput, StrLen(vText)*2*2)
for vKey in oArray
vOutput .= oArray.item(vKey) "`t" vKey "`r`n"
oArray := ""
return vOutput
}
;==================================================
JEE_StrRemoveDupsCIA(ByRef vText)
{
oArray := {}
Loop, Parse, vText, `n, `r
if !oArray.HasKey("" A_LoopField)
oArray["" A_LoopField] := 1
vOutput := ""
VarSetCapacity(vOutput, StrLen(vText)*2+2)
for vKey in oArray
vOutput .= vKey "`n"
oArray := ""
return SubStr(vOutput, 1, -1)
}
;==================================================
;list line frequency ('CID', case insensitive, date creation order)
JEE_StrRemoveDupsCID(ByRef vText)
{
oArray := ComObjCreate("Scripting.Dictionary"), oArray2 := {}
Loop, Parse, vText, `n, `r
if !oArray2.HasKey("" A_LoopField)
oArray.item("" A_LoopField) := "", oArray2["" A_LoopField] := ""
vOutput := ""
VarSetCapacity(vOutput, StrLen(vText)*2*2)
for vKey in oArray
vOutput .= vKey "`n"
oArray := oArray2 := ""
return SubStr(vOutput, 1, -1)
}
;==================================================
;list line frequency ('CID', case insensitive, date creation order)
;roughly equal speed alternative to JEE_StrRemoveDupsCID based on JEE_StrFreqCID
JEE_StrRemoveDupsCID2(ByRef vText)
{
oArray := {}, oArray2 := {}
StrReplace(vText, "`n", "", vCount)
oArray.SetCapacity(vCount+1)
oArray2.SetCapacity(vCount+1)
Loop, Parse, vText, `n, `r
if !oArray.HasKey("" A_LoopField)
oArray["" A_LoopField] := "", oArray2.Push("" A_LoopField)
vOutput := ""
VarSetCapacity(vOutput, StrLen(vText)*2+2)
for _, vValue in oArray2
vOutput .= vValue "`n"
oArray := oArray2 := ""
return SubStr(vOutput, 1, -1)
}
;==================================================
JEE_StrRemoveDupsCSA(ByRef vText, vCSWithinCI=0)
{
vOutput := vText
Sort, vOutput, U CS
;e.g. case sensitive: 'A,B,C,a,b,c'
;e.g. 'CS within CI': 'A,a,B,b,C,c'
if vCSWithinCI
Sort, vOutput, F JEE_SortCasIns
return vOutput
}
;==================================================
;list line frequency ('CSD', case sensitive, date creation order)
JEE_StrRemoveDupsCSD(ByRef vText)
{
oArray := ComObjCreate("Scripting.Dictionary")
Loop, Parse, vText, `n, `r
if !oArray.Exists("" A_LoopField)
oArray.item("" A_LoopField) := ""
vOutput := ""
VarSetCapacity(vOutput, StrLen(vText)*2+2)
for vKey in oArray
vOutput .= vKey "`n"
oArray := ""
return SubStr(vOutput, 1, -1)
}
;==================================================
;17:17 11/05/2017
;stage 1, list A:
;for each item, add key with value 1
;stage 2, list B:
;if item exists and has value 1, set value to 0
;if item exists and has value 0, do nothing
;if item doesn't exist, add key with value - 1
;stage 3:
;1/0/-1 = unique to A/present in both (in the list A's order)/unique to B
;==================================================
;list line frequency ('CIA', case insensitive, alphabetical order)
JEE_StrListCompareCIA(ByRef vText1, ByRef vText2, ByRef vOutputAB, ByRef vOutputA, ByRef vOutputB)
{
oArray := {}
StrReplace(vText1, "`n", "", vCount1)
StrReplace(vText2, "`n", "", vCount2)
vMax := StrLen(vNum1) > StrLen(vNum2) ? StrLen(vNum1) : StrLen(vNum2)
oArray.SetCapacity(vCount1+vCount2+2)
Loop, Parse, vText1, `n, `r
if !oArray.HasKey("" A_LoopField)
oArray["" A_LoopField] := 1
Loop, Parse, vText2, `n, `r
if !oArray.HasKey("" A_LoopField)
oArray["" A_LoopField] := -1
else if (oArray["" A_LoopField] = 1)
oArray["" A_LoopField] := 0
VarSetCapacity(vOutputAB, vMax*2)
VarSetCapacity(vOutputA, vMax*2)
VarSetCapacity(vOutputB, vMax*2)
for vKey, vValue in oArray
if (vValue = 0)
vOutputAB .= vKey "`r`n"
else if (vValue = 1)
vOutputA .= vKey "`r`n"
else
vOutputB .= vKey "`r`n"
oArray := ""
}
;==================================================
;list line frequency ('CID', case insensitive, date creation order)
JEE_StrListCompareCID(ByRef vText1, ByRef vText2, ByRef vOutputAB, ByRef vOutputA, ByRef vOutputB)
{
oArray := ComObjCreate("Scripting.Dictionary"), oArray2 := {}
Loop, Parse, vText1, `n, `r
if !oArray2.HasKey("" A_LoopField)
oArray.item("" A_LoopField) := 1, oArray2["" A_LoopField] := "" A_LoopField
Loop, Parse, vText2, `n, `r
if !oArray2.HasKey("" A_LoopField)
oArray.item("" A_LoopField) := -1, oArray2["" A_LoopField] := "" A_LoopField
else if (oArray.item("" oArray2["" A_LoopField]) = 1)
oArray.item("" A_LoopField) := 0
VarSetCapacity(vOutputAB, vMax*2)
VarSetCapacity(vOutputA, vMax*2)
VarSetCapacity(vOutputB, vMax*2)
for _, vValue in oArray2
if ((vValue2 := oArray.item("" vValue)) = 0)
vOutputAB .= vValue "`r`n"
else if (vValue2 = 1)
vOutputA .= vValue "`r`n"
else
vOutputB .= vValue "`r`n"
oArray := oArray2 := ""
}
;==================================================
;list line frequency ('CID', case insensitive, date creation order)
;slower alternative to JEE_StrListCompareCID
JEE_StrListCompareCID2(ByRef vText1, ByRef vText2, ByRef vOutputAB, ByRef vOutputA, ByRef vOutputB)
{
oArray := {}, oArray2 := {}
StrReplace(vText1, "`n", "", vCount1)
StrReplace(vText2, "`n", "", vCount2)
oArray.SetCapacity(vCount1+vCount2+2)
oArray2.SetCapacity(vCount1+vCount2+2)
Loop, Parse, vText1, `n, `r
if !oArray.HasKey("" A_LoopField)
oArray["" A_LoopField] := 1, oArray2.Push("" A_LoopField)
Loop, Parse, vText2, `n, `r
if !oArray.HasKey("" A_LoopField)
oArray["" A_LoopField] := -1, oArray2.Push("" A_LoopField)
else if (oArray["" A_LoopField] = 1)
oArray["" A_LoopField] := 0
vOutput := ""
VarSetCapacity(vOutputAB, vMax*2)
VarSetCapacity(vOutputA, vMax*2)
VarSetCapacity(vOutputB, vMax*2)
for _, vValue in oArray2
if ((vValue2 := oArray["" vValue]) = 0)
vOutputAB .= vValue "`r`n"
else if (vValue2 = 1)
vOutputA .= vValue "`r`n"
else
vOutputB .= vValue "`r`n"
oArray := oArray2 := ""
}
;==================================================
;list line frequency ('CSA', case sensitive, alphabetical order)
JEE_StrListCompareCSA(ByRef vText1, ByRef vText2, ByRef vOutputAB, ByRef vOutputA, ByRef vOutputB, vCSWithinCI=0)
{
oArray := ComObjCreate("Scripting.Dictionary")
vText1X := StrReplace(vText1, "`r`n", "`n")
vText1X := StrReplace(vText2, "`r`n", "`n")
Sort, vText1X, CS
Sort, vText2X, CS
;e.g. case sensitive: 'A,B,C,a,b,c'
;e.g. 'CS within CI': 'A,a,B,b,C,c'
if vCSWithinCI
{
Sort, vText1X, F JEE_SortCasIns
Sort, vText2X, F JEE_SortCasIns
}
Loop, Parse, vText1X, `n, `r
if !oArray.Exists("" A_LoopField)
oArray.item("" A_LoopField) := 1
Loop, Parse, vText2X, `n, `r
if !oArray.Exists("" A_LoopField)
oArray.item("" A_LoopField) := -1
else if (oArray.item("" A_LoopField) = 1)
oArray.item("" A_LoopField) := 0
VarSetCapacity(vOutputAB, vMax*2)
VarSetCapacity(vOutputA, vMax*2)
VarSetCapacity(vOutputB, vMax*2)
for vKey in oArray
if ((vValue := oArray.item(vKey)) = 0)
vOutputAB .= vKey "`r`n"
else if (vValue = 1)
vOutputA .= vKey "`r`n"
else
vOutputB .= vKey "`r`n"
oArray := ""
}
;==================================================
;list line frequency ('CSD', case sensitive, date creation order)
JEE_StrListCompareCSD(ByRef vText1, ByRef vText2, ByRef vOutputAB, ByRef vOutputA, ByRef vOutputB)
{
oArray := ComObjCreate("Scripting.Dictionary")
Loop, Parse, vText1, `n, `r
if !oArray.Exists("" A_LoopField)
oArray.item("" A_LoopField) := 1
Loop, Parse, vText2, `n, `r
if !oArray.Exists("" A_LoopField)
oArray.item("" A_LoopField) := -1
else if (oArray.item("" A_LoopField) = 1)
oArray.item("" A_LoopField) := 0
VarSetCapacity(vOutputAB, vMax*2)
VarSetCapacity(vOutputA, vMax*2)
VarSetCapacity(vOutputB, vMax*2)
for vKey in oArray
if ((vValue := oArray.item(vKey)) = 0)
vOutputAB .= vKey "`r`n"
else if (vValue = 1)
vOutputA .= vKey "`r`n"
else
vOutputB .= vKey "`r`n"
oArray := ""
}
;==================================================
JEE_SortCasIns(vTextA, vTextB, vOffset) ;for use with AHK's Sort command
{
vSCS := A_StringCaseSense
StringCaseSense, Off
vRet := ("" vTextA) > ("" vTextB) ? 1 : ("" vTextA) < ("" vTextB) ? -1 : -vOffset
StringCaseSense, % vSCS
return vRet
}
;==================================================