Hi!
array := ["one", "two", "three"]
Is there really no direct way to check if "two" is an element of the array (so that a loop must be used)?
No IfIn for a simple array?
-
- Posts: 493
- Joined: 24 Aug 2016, 03:34
-
- Posts: 264
- Joined: 09 Mar 2019, 01:52
- Location: Germany
- Contact:
Re: No IfIn for a simple array?
Yesterday I searched exactly for the same problem and found this:
https://www.autohotkey.com/boards/viewtopic.php?t=23286
But it didn't work for me. HTH you.
https://www.autohotkey.com/boards/viewtopic.php?t=23286
But it didn't work for me. HTH you.
Re: No IfIn for a simple array?
here is a pretty direct way:newbieforever wrote: ↑30 Mar 2019, 05:42Hi!
array := ["one", "two", "three"]
Is there really no direct way to check if "two" is an element of the array (so that a loop must be used)?
v2
Code: Select all
array := ["one", "two", "three"]
for key, value in array
if InStr(value,"two")
MsgBox("The string > " value " < was found.")
return
Code: Select all
array := ["one", "two", "three"]
for key, value in array
if InStr(value,"two")
MsgBox % "The string > " value " < was found."
return
v2
Code: Select all
array := ["one", "two", "three"]
, InStr(array[2], "two") ? MsgBox("The string > " array[2] " < was found.") : MsgBox("The string > two < was NOT found.")
return
v1
Code: Select all
array := ["one", "two", "three"]
if (InStr(array[2], "two"))
MsgBox % The string > " array[2] " < was found."
else
MsgBox % "The string > two < was NOT found."
return
Last edited by Tigerlily on 30 Mar 2019, 06:32, edited 1 time in total.
-TL
-
- Posts: 493
- Joined: 24 Aug 2016, 03:34
Re: No IfIn for a simple array?
Thank you, Kobaltaugte & Tigerlily!
So I summarize: There is no "direct way" (in the sense of my question)...
So I summarize: There is no "direct way" (in the sense of my question)...
Re: No IfIn for a simple array?
Hm.. my second method was extremely direct...newbieforever wrote: ↑30 Mar 2019, 06:30Thank you, Kobaltaugte & Tigerlily!
So I summarize: There is no "direct way" (in the sense of my question)...
this may be the most direct way:
https://www.autohotkey.com/boards/viewtopic.php?f=6&t=63173&p=270087
-TL
Re: No IfIn for a simple array?
You could even do this in v2:
resolves to 1, meaning string was found!
Code: Select all
MsgBox(InStr(array := ["one", "two", "three"][2], "two"))
-TL
-
- Posts: 493
- Joined: 24 Aug 2016, 03:34
Re: No IfIn for a simple array?
@Tigerlily: But of course this was meant to mean that you don't know in advance that it is the 2nd element.
Or, for a better understanding of my question:
array := ["one", "three", "two"]
Is there really no direct way to check if "four" is an element of the array (so that a loop must be used)?
Or, for a better understanding of my question:
array := ["one", "three", "two"]
Is there really no direct way to check if "four" is an element of the array (so that a loop must be used)?
Re: No IfIn for a simple array?
I'm a little confused .. are you asking if you can directly check WITHOUT a loop? or WITH a loop?newbieforever wrote: ↑30 Mar 2019, 07:02@Tigerlily: But of course this was meant to mean that you don't know in advance that it is the 2nd element.
Or, for a better understanding of my question:
array := ["one", "three", "two"]
Is there really no direct way to check if "four" is an element of the array (so that a loop must be used)?
what do you mean by direct way?
-TL
-
- Posts: 493
- Joined: 24 Aug 2016, 03:34
Re: No IfIn for a simple array?
Direct = without a loop.
Re: No IfIn for a simple array?
v2
Code: Select all
i := 1, InStr(array := ["one", "two", "three"][i], "two") ? MsgBox("found in index # " i ) : i++, InStr(array := ["one", "two", "three"][i], "two") ? MsgBox("found in index # " i ) : i++, InStr(array := ["one", "two", "three"][i], "two") ? MsgBox("found in index # " i ) : i++, InStr(array := ["one", "two", "three"][i], "two") ? MsgBox("string not found in any index") : { }
Last edited by Tigerlily on 30 Mar 2019, 07:52, edited 2 times in total.
-TL
Re: No IfIn for a simple array?
If you could put the values as keys (they must be unique) then you could use .HasKey()
ciao
toralf
toralf
Re: No IfIn for a simple array?
v2
Code: Select all
a:=["one","five","two"],(InStr(a[1],"four")!=0)||(InStr(a[2],"four")!=0)||(InStr(a[3],"four")!=0)||(InStr(a[4],"four")!= 0)?MsgBox("found"):MsgBox("not found")
-TL
Re: No IfIn for a simple array?
Without loop (using the strrept string hack by jeeswg):
As for me, I think the better way is using a loop.
Code: Select all
array := ["one", "two", "three"]
MsgBox % hasValue(array, "two") "`n"
. hasValue(array, "Two", false) "`n"
. hasValue(array, "Two", true) "`n"
. hasValue(array, "thre") "`n"
. hasValue(array, "one") "`n"
hasValue(arr, value, caseSensitive:=false) {
return !!InStr(Format("|" StrReplace(Format("{:" ObjCount(arr) "}", "")," ","{:}|"), arr*), "|" value "|", caseSensitive)
}
-
- Posts: 264
- Joined: 09 Mar 2019, 01:52
- Location: Germany
- Contact:
Re: No IfIn for a simple array?
The solution of @toralf I found here yesterday to.
https://stackoverflow.com/questions/33591667/how-to-check-if-string-is-contained-in-an-array-in-autohotkey/33593563
But I have the same issue. I'm iterating though hundreds/thousand of XMLs. My goal was to push on specific element in an array. And then going on with
I didn't find a proper solution without looping through the array. But as you can imagine after a while it took a while. =)
https://stackoverflow.com/questions/33591667/how-to-check-if-string-is-contained-in-an-array-in-autohotkey/33593563
But I have the same issue. I'm iterating though hundreds/thousand of XMLs. My goal was to push on specific element in an array. And then going on with
Code: Select all
If "new element" is in array {
do this
}
Re: No IfIn for a simple array?
can you use querySelector/querySelectorAll?Kobaltauge wrote: ↑30 Mar 2019, 07:54The solution of @toralf I found here yesterday to.
https://stackoverflow.com/questions/33591667/how-to-check-if-string-is-contained-in-an-array-in-autohotkey/33593563
But I have the same issue. I'm iterating though hundreds/thousand of XMLs. My goal was to push on specific element in an array. And then going on withI didn't find a proper solution without looping through the array. But as you can imagine after a while it took a while. =)Code: Select all
If "new element" is in array { do this }
Code: Select all
myDoc.querySelector("[*|id='toto']")
Last edited by Tigerlily on 30 Mar 2019, 08:04, edited 1 time in total.
-TL
Re: No IfIn for a simple array?
If the check (IfIn) is required more often. You can have the normal Array as you have right now and create a Value Array where the values are the keys. Then you can check in the values Array. Of course, you’ Have to keep the two arrays in sync.
Ps: the value Array could have the xml path as values
Ps: the value Array could have the xml path as values
ciao
toralf
toralf
-
- Posts: 264
- Joined: 09 Mar 2019, 01:52
- Location: Germany
- Contact:
Re: No IfIn for a simple array?
Thank you for your feedback.Tigerlily wrote:
I don't want to hijack this thread, but I don't know how to get your solution to run.
Additionally, hope I understand what you are doing, I don't want to find a specific value in a bunch of XMLs. I'll try to search in all the XML for double values.
Therefor I tried to read all the values in one array and search for an existing one.
But it doesn't matter, don't waste more time on my issue. I found another solution. And if I need your solution in future I'll post a new thread.
Thank you. This was exactly what I tried, but I didn't get it to work. So I found another soultion involving a csv file and Exceltoralf wrote:
I have still so much to learn.
Re: No IfIn for a simple array?
You will have to use the xml values as keys in the AHK associated Array
ciao
toralf
toralf
Re: No IfIn for a simple array?
regular ahk arrays u gotta loop over to find specific values, theres no way around that
u can stuff ur values in an associative array like toralf suggested, but ull lose the order of insertion and case sensitivity. identical values will also overwrite previous one.
if u need to preserve these properties, ull have to implement ur own ordered hashmap
u can stuff ur values in an associative array like toralf suggested, but ull lose the order of insertion and case sensitivity. identical values will also overwrite previous one.
if u need to preserve these properties, ull have to implement ur own ordered hashmap
Re: No IfIn for a simple array?
Why are you using arrays instead of xpath if it's an XML document
We are troubled on every side‚ yet not distressed; we are perplexed‚
but not in despair; Persecuted‚ but not forsaken; cast down‚ but not destroyed;
Telegram is the best way to reach me
https://t.me/ttnnkkrr
If you have forum suggestions please submit a
Check Out WebWriter
but not in despair; Persecuted‚ but not forsaken; cast down‚ but not destroyed;
Telegram is the best way to reach me
https://t.me/ttnnkkrr
If you have forum suggestions please submit a
Check Out WebWriter