thank you
by HotKeyIt:
!!InStr(del list del, del item del)
how do I convert this ?
if var in hello,,world!,oof
note that ,, is escape sequence for literal comma
this works for "hello,world!", but also works for "world!", which is incorrect
InStr(del "hello,world!,oof" del, del "hello,world!" del) ;1
InStr(del "hello,world!,oof" del, del "world!" del) ;7
by guest3456:
if RegExMatch(var, "^exe$|^bat$|^com$")
wow, that's some regex
Code: Select all
var:="monday"
if (var ~= "i)^Mon$|^Tue$|^Wed$|^Thu$|^Fri$|^Sat$|^Sun$")
msgbox 1
else
msgbox 2
; 2
perfect, we've got it
I will have to learn how to escape regex...
and the user too... "why does it have unintended results ?"
-----
I've thought about it: behold, my bias
inline Map() : those who don't care about performance won't notice,
those who care about performance will copy paste it to somewhere else to get best performance
and if they(or I) see someone else's code, it better be a Map() that they can copy to somewhere else than a regex, which takes more time to convert to Map() for best performance
those who care about readability should use a custom function, because this regex also hard to read, and it gets worse with regex escape sequence
I will provide such function as a suggestion
---
regex is readable inline "^Mon$|^Tue$" vs Map shows the name of the group: daysOfTheWeek
regex is fast
---
here are my benchmarks:
Code: Select all
ListLines 0
#SingleInstance force
SendMode "Input" ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir A_ScriptDir ; Ensures a consistent starting directory.
#KeyHistory 0
;consider, true, false, lowercase, longer strings;
;consider loop x iterations
; var:="mon"
; var:="sun"
; var:="non"
; var:="MoN"
; var:="abcdefghijklmnopqrstuvwxyz"
var:="monday" ;this will fail it
Frequency := 0
Before := 0
After := 0
DllCall("QueryPerformanceFrequency", "Int64*", Frequency)
DllCall("QueryPerformanceCounter", "Int64*", Before)
QPC(1)
sleep 1000 ;0.988971
testN := QPC(0), QPC(1)
sum1:=0
loop 1234567 {
if (var ~= "i)^Mon$|^Tue$|^Wed$|^Thu$|^Fri$|^Sat$|^Sun$") {
sum1++
}
}
test1 := QPC(0), QPC(1)
sum2:=0
loop 1234567 {
if (Map("mon",1,"tue",1,"wed",1,"thu",1,"fri",1,"sat",1,"sun",1).Has(StrLower(var))) {
sum2++
}
}
test2 := QPC(0), QPC(1)
daysOfTheWeek:=Map()
daysOfTheWeek.CaseSense:="Off"
daysOfTheWeek.Set("mon",1,"tue",1,"wed",1,"thu",1,"fri",1,"sat",1,"sun",1)
sum3:=0
loop 1234567 {
if (daysOfTheWeek.Has(var)) {
sum3++
}
}
test3 := QPC(0), QPC(1)
d "`"" var "`": " array_p([test1, test2, test3, sum1, sum2, sum3, testN])
; "mon": [0.43705080000000002, 3.2957676, 0.37818469999999998, 1234567, 1234567, 1234567, 0.99087840000000005]
; "sun": [0.50800420000000002, 3.3012972, 0.36907889999999999, 1234567, 1234567, 1234567, 1.0067090999999999]
; "non": [0.74503560000000002, 4.1980212000000003, 0.25033460000000002, 0, 0, 0, 1.0015638]
; "MoN": [0.4249423, 3.2777126000000001, 0.39381719999999998, 1234567, 1234567, 1234567, 0.99355260000000001]
; "abcdefghijklmnopqrstuvwxyz": [3.3660597999999999, 3.2118014000000001, 0.25318950000000001, 0, 0, 0, 0.98656829999999995]
; "mon": regex is surprisingly fast
; but saving Map() to variable is slightly faster
; I realize that remaking the Map() every time is extremely slow
; "sun": since |sun is the last |OR in the regex, it's slightly slower, while Map() is the same
; "non": regex a bit slower when no match, Map() a bit faster!, I don't understand how
; and somehow remaking Map() becomes a bit slower
; "MoN": this is testing uppercase: I think there's no difference since to do case insensitive, you have to turn to lowercase anyways
; "abcdefghijklmnopqrstuvwxyz": regex WAY slower, because it's like InStr() every possibility, while for Map() it's no match
;FIXED REGEX
; "mon": [0.48089080000000001, 3.3009287, 0.3795789, 1234567, 1234567, 1234567, 1.0023203999999999]
; "non": [0.39596930000000002, 4.2182253000000003, 0.2509229, 0, 0, 0, 0.98815609999999998]
; "MoN": [0.4906201, 4.3829558000000004, 0.38037850000000001, 1234567, 1234567, 1234567, 1.0023477000000001]
; "abcdefghijklmnopqrstuvwxyz": [0.40325549999999999, 3.1940783000000001, 0.24873770000000001, 0, 0, 0, 0.99239500000000003]
; "monday": [0.45882659999999997, 3.1579256, 0.25448559999999998, 0, 0, 0, 0.9965735]
; "sun": [0.58207660000000006, 3.3259618999999998, 0.36763050000000003, 1234567, 1234567, 1234567, 0.99519139999999995]
Exitapp
QPC(R := 0)
{
static P := 0, F := 0, Q := DllCall("QueryPerformanceFrequency", "Int64P", F)
return ! DllCall("QueryPerformanceCounter", "Int64P", Q) + (R ? (P := Q) / F : (Q - P) / F)
}
return
f3::Exitapp
;FIXED REGEX
; "mon": [0.48089080000000001, 3.3009287, 0.3795789, 1234567, 1234567, 1234567, 1.0023203999999999]
; "non": [0.39596930000000002, 4.2182253000000003, 0.2509229, 0, 0, 0, 0.98815609999999998]
; "MoN": [0.4906201, 4.3829558000000004, 0.38037850000000001, 1234567, 1234567, 1234567, 1.0023477000000001]
; "abcdefghijklmnopqrstuvwxyz": [0.40325549999999999, 3.1940783000000001, 0.24873770000000001, 0, 0, 0, 0.99239500000000003]
; "monday": [0.45882659999999997, 3.1579256, 0.25448559999999998, 0, 0, 0, 0.9965735]
; "sun": [0.58207660000000006, 3.3259618999999998, 0.36763050000000003, 1234567, 1234567, 1234567, 0.99519139999999995]
regex is never really slower than Map()
regex can be faster if
if var in has less elements
while inline Map() is VERY slow
I have a bias against regex (regex=slow), I can't forget this is regex if I have to edit the list