Page 1 of 1

Store an array name and contents at a given position

Posted: 04 May 2020, 03:55
by PuzzledGreatly
I want to be able to search through multiple arrays and when the contents match the search item then store which array holds the search item and what position it is at. I thought the following would work:

Code: Select all

GetArray()
{
	Global
	GuiControlget, BK, pos

	SQC := ""
	loop, 50
	{
		if instr(AA[A_index], BKX) && instr(AA[A_index], BKY)
		SQC := "AA[" A_index "]"
	
		if SQC
		break
	}
	test := SQC
	msgbox, 4096, Square, %SQC%: %test%
}
I thought test would contain the value of the array but currently it is showing the same result as SQC. What have I done wrong? Ii I use:

Code: Select all

SQC := AA[A_index]
Then, as expected, I get the contents of the array twice. Whatever I try I can't get SQC to show the array and test to show the contents of the array. Will someone please put me out of my misery as I've been at this for hours now. Thanks.

Re: Store an array name and contents at a given position

Posted: 04 May 2020, 04:25
by BoBo
AFAIKS, your inStr() 'needles' are variables, but I can't see where you've declared them in the first place, and if not set globally, how their content should make it to the function??

Re: Store an array name and contents at a given position

Posted: 04 May 2020, 04:57
by hasantr
I think I couldn't understand what he wanted. But if I got it right.

Code: Select all

SQC := "AA[" A_index "]" .  AA[ A_index ]

Re: Store an array name and contents at a given position

Posted: 04 May 2020, 05:22
by PuzzledGreatly
Thanks for the replies. The function is part of a larger code and the contents of the variables is not an issue. The issue is how to store the array and then retrieve its content. Thanks for the code:

Code: Select all

SQC := "AA[" A_index "]" .  AA[ A_index ]
Unfortunately that produces the same value in both SQC and test. Can someone explain why, in my original code test produces aa[A_index] and not the value stored in aa[A_index]? Thanks.

Re: Store an array name and contents at a given position

Posted: 04 May 2020, 05:36
by swagfag
because SCQ := "AA[1]" contains a string and assigning it to another variable doesnt dereference the string, it just copies it over

Re: Store an array name and contents at a given position

Posted: 04 May 2020, 06:03
by boiler
BoBo wrote:
04 May 2020, 04:25
AFAIKS, your inStr() 'needles' are variables, but I can't see where you've declared them in the first place, and if not set globally, how their content should make it to the function??
BKX and BKY are assigned values when this line is executed:

Code: Select all

GuiControlget, BK, pos

Re: Store an array name and contents at a given position

Posted: 04 May 2020, 06:17
by boiler
The reason your approach doesn’t work was explained by swagfag. So while you can’t accomplish your goal by dereferencing a string containing an array reference as you can with a simple variable name, this accomplishes what you described (as you probably are already aware):

Code: Select all

 GetArray()
{
	Global
	GuiControlget, BK, pos

	SQC := ""
	loop, 50
	{
		if instr(AA[A_index], BKX) && instr(AA[A_index], BKY)
		{
			 SQC := "AA[" A_index "]"
			 test := AA[A_Index]
			 break
		}
	}
	msgbox, 4096, Square, %SQC%: %test%
}

Re: Store an array name and contents at a given position

Posted: 08 May 2020, 15:48
by PuzzledGreatly
Thanks for the replies. In the end I gave up using arrays as I couldn't get it to work as I expected .