if var in/contains comma-separated list/array
Posted: 31 Jan 2018, 00:59
At the moment in AHK v1, we have:
if var contains Mon,Tue,Wed,Thu,Fri,Sat,Sun
In AHK v2, this would probably become:
if var contains ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"]
Which is far less convenient.
Such lists are inconvenient to type, although hotkeys to prepare the text are possible.
This would be made more bulky by script correctors that automatically separate parameters.
if var contains ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
(You might say 'don't use a corrector', but in general I've found that it's better to use one than not to use one, and that it's simpler to apply it to all arrays than to try to make exceptions.)
This would be less inconvenient to type, but still a bit bulky:
if var contains StrSplit("Mon,Tue,Wed,Thu,Fri,Sat,Sun", ",")
A workaround:
if var contains CSL("Mon,Tue,Wed,Thu,Fri,Sat,Sun")
if var contains PSL("Mon|Tue|Wed|Thu|Fri|Sat|Sun")
CSL/PSL aren't too bad, but using that every time?
An alternative. Something like this perhaps!?
if var containsCSL "Mon,Tue,Wed,Thu,Fri,Sat,Sun"
Some further alternatives.
if StrContains(var, "Mon,Tue,Wed,Thu,Fri,Sat,Sun", "D,")
if StrContains(var, "Mon|Tue|Wed|Thu|Fri|Sat|Sun", "D|")
if StrContainsCSL(var, "Mon,Tue,Wed,Thu,Fri,Sat,Sun")
Also, there's RegExMatch. But this has the drawbacks of: checking for characters that need escaping, having to use pipes, being slow.
if RegExMatch(var, "Mon|Tue|Wed|Thu|Fri|Sat|Sun")
Btw I would generally never use [] to define an array, I would always use something like CSL if available:
oArray := ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"]
oArray := CSL("Mon,Tue,Wed,Thu,Fri,Sat,Sun")
(Although for numbers, the [] syntax does remain useful.)
oArray := [1,2,3,4,5,6,7]
I believe that most people including myself: (a) see delimiter-separated strings as a bit old-fashioned cf. arrays, (b) see it as hard to justify interpreting a string as comma-delimited by default. However, the comma/pipe-separated list remains very useful.
I believe also that in AHK v1, and in AHK v2 alpha at the moment, arrays are almost never used as input parameters for functions, and so key issues, and the practical reality of their day-to-day use, haven't really been considered yet. Because it's new territory, and because it takes some time to work out how to develop a new syntax, I am concerned that it's the sort of thing where people might not quite get the syntax right and potentially end up saying: we need to fix this in AHK v3.
Thanks for reading.
if var contains Mon,Tue,Wed,Thu,Fri,Sat,Sun
In AHK v2, this would probably become:
if var contains ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"]
Which is far less convenient.
Such lists are inconvenient to type, although hotkeys to prepare the text are possible.
This would be made more bulky by script correctors that automatically separate parameters.
if var contains ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
(You might say 'don't use a corrector', but in general I've found that it's better to use one than not to use one, and that it's simpler to apply it to all arrays than to try to make exceptions.)
This would be less inconvenient to type, but still a bit bulky:
if var contains StrSplit("Mon,Tue,Wed,Thu,Fri,Sat,Sun", ",")
A workaround:
if var contains CSL("Mon,Tue,Wed,Thu,Fri,Sat,Sun")
if var contains PSL("Mon|Tue|Wed|Thu|Fri|Sat|Sun")
CSL/PSL aren't too bad, but using that every time?
An alternative. Something like this perhaps!?
if var containsCSL "Mon,Tue,Wed,Thu,Fri,Sat,Sun"
Some further alternatives.
if StrContains(var, "Mon,Tue,Wed,Thu,Fri,Sat,Sun", "D,")
if StrContains(var, "Mon|Tue|Wed|Thu|Fri|Sat|Sun", "D|")
if StrContainsCSL(var, "Mon,Tue,Wed,Thu,Fri,Sat,Sun")
Also, there's RegExMatch. But this has the drawbacks of: checking for characters that need escaping, having to use pipes, being slow.
if RegExMatch(var, "Mon|Tue|Wed|Thu|Fri|Sat|Sun")
Btw I would generally never use [] to define an array, I would always use something like CSL if available:
oArray := ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"]
oArray := CSL("Mon,Tue,Wed,Thu,Fri,Sat,Sun")
(Although for numbers, the [] syntax does remain useful.)
oArray := [1,2,3,4,5,6,7]
I believe that most people including myself: (a) see delimiter-separated strings as a bit old-fashioned cf. arrays, (b) see it as hard to justify interpreting a string as comma-delimited by default. However, the comma/pipe-separated list remains very useful.
I believe also that in AHK v1, and in AHK v2 alpha at the moment, arrays are almost never used as input parameters for functions, and so key issues, and the practical reality of their day-to-day use, haven't really been considered yet. Because it's new territory, and because it takes some time to work out how to develop a new syntax, I am concerned that it's the sort of thing where people might not quite get the syntax right and potentially end up saying: we need to fix this in AHK v3.
Thanks for reading.