No IfIn for a simple array?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
newbieforever
Posts: 493
Joined: 24 Aug 2016, 03:34

No IfIn for a simple array?

30 Mar 2019, 05:42

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)?
Kobaltauge
Posts: 264
Joined: 09 Mar 2019, 01:52
Location: Germany
Contact:

Re: No IfIn for a simple array?

30 Mar 2019, 05:56

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.
User avatar
Tigerlily
Posts: 377
Joined: 04 Oct 2018, 22:31

Re: No IfIn for a simple array?

30 Mar 2019, 06:17

newbieforever wrote:
30 Mar 2019, 05:42
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)?
here is a pretty direct way:

v2

Code: Select all

array := ["one", "two", "three"]
for key, value in array
	if InStr(value,"two")
		MsgBox("The string  > " value " <  was found.")
return
v1

Code: Select all

array := ["one", "two", "three"]
for key, value in array
	if InStr(value,"two")
		MsgBox % "The string  > " value " <  was found."
return
You could also do

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
newbieforever
Posts: 493
Joined: 24 Aug 2016, 03:34

Re: No IfIn for a simple array?

30 Mar 2019, 06:30

Thank you, Kobaltaugte & Tigerlily!

So I summarize: There is no "direct way" (in the sense of my question)...
User avatar
Tigerlily
Posts: 377
Joined: 04 Oct 2018, 22:31

Re: No IfIn for a simple array?

30 Mar 2019, 06:34

newbieforever wrote:
30 Mar 2019, 06:30
Thank you, Kobaltaugte & Tigerlily!

So I summarize: There is no "direct way" (in the sense of my question)...
Hm.. my second method was extremely direct...

this may be the most direct way:

https://www.autohotkey.com/boards/viewtopic.php?f=6&t=63173&p=270087
-TL
User avatar
Tigerlily
Posts: 377
Joined: 04 Oct 2018, 22:31

Re: No IfIn for a simple array?

30 Mar 2019, 06:39

You could even do this in v2:

Code: Select all

MsgBox(InStr(array := ["one", "two", "three"][2], "two"))
resolves to 1, meaning string was found!
-TL
newbieforever
Posts: 493
Joined: 24 Aug 2016, 03:34

Re: No IfIn for a simple array?

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)?
User avatar
Tigerlily
Posts: 377
Joined: 04 Oct 2018, 22:31

Re: No IfIn for a simple array?

30 Mar 2019, 07:15

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)?
I'm a little confused .. are you asking if you can directly check WITHOUT a loop? or WITH a loop?

what do you mean by direct way?
-TL
newbieforever
Posts: 493
Joined: 24 Aug 2016, 03:34

Re: No IfIn for a simple array?

30 Mar 2019, 07:18

Direct = without a loop.
User avatar
Tigerlily
Posts: 377
Joined: 04 Oct 2018, 22:31

Re: No IfIn for a simple array?

30 Mar 2019, 07:25

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
toralf
Posts: 868
Joined: 27 Apr 2014, 21:08
Location: Germany

Re: No IfIn for a simple array?

30 Mar 2019, 07:33

If you could put the values as keys (they must be unique) then you could use .HasKey()
ciao
toralf
User avatar
Tigerlily
Posts: 377
Joined: 04 Oct 2018, 22:31

Re: No IfIn for a simple array?

30 Mar 2019, 07:41

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
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: No IfIn for a simple array?

30 Mar 2019, 07:52

Without loop (using the strrept string hack by jeeswg):

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)
}
As for me, I think the better way is using a loop.
my scripts
Kobaltauge
Posts: 264
Joined: 09 Mar 2019, 01:52
Location: Germany
Contact:

Re: No IfIn for a simple array?

30 Mar 2019, 07:54

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

Code: Select all

If "new element" is in array {
do this
}
I didn't find a proper solution without looping through the array. But as you can imagine after a while it took a while. =)
User avatar
Tigerlily
Posts: 377
Joined: 04 Oct 2018, 22:31

Re: No IfIn for a simple array?

30 Mar 2019, 08:02

Kobaltauge wrote:
30 Mar 2019, 07:54
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

Code: Select all

If "new element" is in array {
do this
}
I didn't find a proper solution without looping through the array. But as you can imagine after a while it took a while. =)
can you use querySelector/querySelectorAll?

Code: Select all

myDoc.querySelector("[*|id='toto']")
Will return an empty string if the object is missing, but if its there will be node[0]
Last edited by Tigerlily on 30 Mar 2019, 08:04, edited 1 time in total.
-TL
toralf
Posts: 868
Joined: 27 Apr 2014, 21:08
Location: Germany

Re: No IfIn for a simple array?

30 Mar 2019, 08:04

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
ciao
toralf
Kobaltauge
Posts: 264
Joined: 09 Mar 2019, 01:52
Location: Germany
Contact:

Re: No IfIn for a simple array?

30 Mar 2019, 13:15

Tigerlily wrote:
Thank you for your feedback.
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.
toralf wrote:
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 Excel :?
I have still so much to learn.
toralf
Posts: 868
Joined: 27 Apr 2014, 21:08
Location: Germany

Re: No IfIn for a simple array?

30 Mar 2019, 14:41

You will have to use the xml values as keys in the AHK associated Array
ciao
toralf
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: No IfIn for a simple array?

30 Mar 2019, 16:23

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
User avatar
tank
Posts: 3127
Joined: 28 Sep 2013, 22:15
Location: CarrolltonTX
Contact:

Re: No IfIn for a simple array?

30 Mar 2019, 16:59

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

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Joey5, mikeyww, Rohwedder, ziru and 284 guests