For Loop on a sequentially numbered array

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Entropy42
Posts: 29
Joined: 11 Dec 2016, 12:34

For Loop on a sequentially numbered array

18 Jan 2017, 00:49

I have a 3 dimensional array with sequentially numbered keys, and the for loop is always grabbing the first key. On every iteration k=1 and pattern is always the 7x2 array in the patterns[1] definition.

Code: Select all

patterns := Object()
patterns[1] := [ [-2,0] , [-1,0] , [1,0] , [2,0] , [0,-1] , [0,-2] , [0,1] ]		; check for 8 gem Ts (only possible from cascades)
	patterns[2] := [ [0,-2] , [0,-1] , [0,-1] , [0,-2] , [-1,0] , [-2,0] , [1,0] ]
	patterns[3] := [ [-2,0] , [-1,0] , [1,0] , [2,0] , [0,1] , [0,2] , [0,-1] ]
	patterns[4] := [ [0,1] , [0,2] , [0,-1] , [0,-2] , [1,0] , [2,0] , [-1,0] ]
	patterns[5] := [ [-2,0] , [-1,0] , [1,0] , [2,0] , [0,-1] , [0,-2] ]				; check for 7 gem Ts
	patterns[6] := [ [0,-2] , [0,-1] , [0,-1] , [0,-2] , [-1,0] , [-2,0] ]
	patterns[7] := [ [-2,0] , [-1,0] , [1,0] , [2,0] , [0,1] , [0,2] ]
	patterns[8] := [ [0,1] , [0,2] , [0,-1] , [0,-2] , [1,0] , [2,0] ]
while patternMatch(board, results, patterns)		
{
	resolveCascades(board)	; found matches resulting from the previous moves, move gems down and check again
	clipboard = % clipboard "`r`n" "Board after cascades:`r`n" . printBoard(board)
}

patternMatch(board, results, patterns)
{
	matchFound := false
	for k, pattern in patterns		; For each pattern, starting with the biggest, loop through the whole grid and try to find it
	{		
		Loop, 8
		{
			y := A_Index
			Loop, 8
			{
				x := A_Index
				col := board[x,y]
				if (col = 0)	; If we are checking an empty square, go to the next square and check no patterns
					continue
			}
		}
	}
	return matchFound
}
ManualColdLock
Posts: 175
Joined: 15 Dec 2016, 04:27

Re: For Loop on a sequentially numbered array

18 Jan 2017, 02:13

There's a problem with your patterns object

Code: Select all

patterns :=[]
patterns[1] := [ [-2,0] , [-1,0] , [1,0] , [2,0] , [0,-1] , [0,-2] , [0,1] ]

for, k,v in Patterns[1]
	msgbox,% "K = " k " & V = " v 

ExitApp
The first Key is 1 since there's no key name assigned and the value is blank because it doesn't know what to do with [-2,0] as a value

This works for Key,Value storing but...

Code: Select all

patterns :=[]
patterns[1] := { -2:0 , -1:0 , 1:0 , 2:0 , 0:-1 , 0:-2 , 0:1 }

for, k,v in Patterns[1]
	msgbox,% "K = " k " & V = " v 

ExitApp
you'll notice they aren't read in order!

Here's what works.

Code: Select all

patterns :=[]
patterns[1] := ["-2|0" ,"-1|0" , "1|0 ", "2|0" , "0|-1" , "0|-2" , "0|1" ]

for, k,v in Patterns[1]
{
StringSplit, Split,v,|
x := Split1
y := Split2
msgbox,% "K = " k " & V = " v "`nX = " x " & Y = " y 
}

ExitApp
Guest

Re: For Loop on a sequentially numbered array

18 Jan 2017, 03:24

ManualColdLock wrote:

Code: Select all

patterns :=[]
patterns[1] := [ [-2,0] , [-1,0] , [1,0] , [2,0] , [0,-1] , [0,-2] , [0,1] ]

for, k,v in Patterns[1]
	msgbox,% "K = " k " & V = " v 

ExitApp
The first Key is 1 since there's no key name assigned and the value is blank because it doesn't know what to do with [-2,0] as a value
k explain this then

Code: Select all

patterns :=[]
patterns[1] := [ [-2,0] , [-1,0] , [1,0] , [2,0] , [0,-1] , [0,-2] , [0,1] ]
MsgBox, % patterns.1.1.1
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: For Loop on a sequentially numbered array

18 Jan 2017, 03:31

ManualColdLock wrote:There's a problem with your patterns object
Not really.

Code: Select all

patterns :=[]
patterns[1] := [ [-2,0] , [-1,0] , [1,0] , [2,0] , [0,-1] , [0,-2] , [0,1] ]

for, k,v in Patterns[1]
	msgbox,% "K = " k " & V = " v[1] "," v[2] 

ExitApp
@ Op, what exactly do you want to achieve?
ManualColdLock
Posts: 175
Joined: 15 Dec 2016, 04:27

Re: For Loop on a sequentially numbered array

18 Jan 2017, 03:48

Thanks Helgef, I was wondering what happened to those arrays!
Entropy42
Posts: 29
Joined: 11 Dec 2016, 12:34

Re: For Loop on a sequentially numbered array

18 Jan 2017, 09:15

I'm making a multi-dimensional array. Each element of the array is a list of offsets, representing a pattern. Each offset is a 2-element array representing x and y. The for loop is supposed to step through the first dimension of the array and just pass the list of offsets along to another function, but it was repeatedly passing the first list. After posting this, I replaced the "for k, pattern in patterns" with "Loop, % patterns.MaxIndex()", and the A_index value wasn't incrementing either. This morning, on a different computer, it is working fine. I don't know if it was my AHK or my debugger, but something was totally screwed up when I posted this. So... solved? I haven't used "for each" much in AHK, so I thought it was simply failing when the array had numeric key values.
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: For Loop on a sequentially numbered array

18 Jan 2017, 12:00

Entropy42 wrote:I have a 3 dimensional array with sequentially numbered keys, and the for loop is always grabbing the first key. On every iteration k=1 and pattern is always the 7x2 array in the patterns[1] definition.

Code: Select all

patterns := Object()
patterns[1] := [ [-2,0] , [-1,0] , [1,0] , [2,0] , [0,-1] , [0,-2] , [0,1] ]		; check for 8 gem Ts (only possible from cascades)
	patterns[2] := [ [0,-2] , [0,-1] , [0,-1] , [0,-2] , [-1,0] , [-2,0] , [1,0] ]
	patterns[3] := [ [-2,0] , [-1,0] , [1,0] , [2,0] , [0,1] , [0,2] , [0,-1] ]
	patterns[4] := [ [0,1] , [0,2] , [0,-1] , [0,-2] , [1,0] , [2,0] , [-1,0] ]
	patterns[5] := [ [-2,0] , [-1,0] , [1,0] , [2,0] , [0,-1] , [0,-2] ]				; check for 7 gem Ts
	patterns[6] := [ [0,-2] , [0,-1] , [0,-1] , [0,-2] , [-1,0] , [-2,0] ]
	patterns[7] := [ [-2,0] , [-1,0] , [1,0] , [2,0] , [0,1] , [0,2] ]
	patterns[8] := [ [0,1] , [0,2] , [0,-1] , [0,-2] , [1,0] , [2,0] ]
while patternMatch(board, results, patterns)		
{
	resolveCascades(board)	; found matches resulting from the previous moves, move gems down and check again
	clipboard = % clipboard "`r`n" "Board after cascades:`r`n" . printBoard(board)
}

patternMatch(board, results, patterns)
{
	matchFound := false
	for k, pattern in patterns		; For each pattern, starting with the biggest, loop through the whole grid and try to find it
	{		
		Loop, 8
		{
			y := A_Index
			Loop, 8
			{
				x := A_Index
				col := board[x,y]
				if (col = 0)	; If we are checking an empty square, go to the next square and check no patterns
					continue
			}
		}
	}
	return matchFound
}
"for k, pattern in patterns" <-- This loops through patterns 1-8. Then inside the loop you make no mention of pattern.

To loop over each element:

Code: Select all

patterns := []
patterns[1] := [ [-2,0], [-1,0], [1, 0], [2, 0], [0,-1], [0,-2], [0, 1] ]
patterns[2] := [ [0,-2], [0,-1], [0,-1], [0,-2], [-1,0], [-2,0], [1, 0] ]
patterns[3] := [ [-2,0], [-1,0], [1, 0], [2, 0], [0, 1], [0, 2], [0,-1] ]
patterns[4] := [ [0, 1], [0, 2], [0,-1], [0,-2], [1, 0], [2, 0], [-1,0] ]
patterns[5] := [ [-2,0], [-1,0], [1, 0], [2, 0], [0,-1], [0,-2] ]
patterns[6] := [ [0,-2], [0,-1], [0,-1], [0,-2], [-1,0], [-2,0] ]
patterns[7] := [ [-2,0], [-1,0], [1, 0], [2, 0], [0, 1], [0, 2] ]
patterns[8] := [ [0, 1], [0, 2], [0,-1], [0,-2], [1, 0], [2, 0] ]
for PatternNumber, Pattern in Patterns
{
	for OffsetNumber, Offset in Pattern
	{
		MsgBox, % "PatternNumber: "	PatternNumber 	"`n"
				. "OffsetNumber: "  OffsetNumber	"`n"
				. "Offset 1: "		Offset[1]		"`n"
				. "Offset 2: "		Offset[2]
	}
}
Entropy42 wrote:After posting this, I replaced the "for k, pattern in patterns" with "Loop, % patterns.MaxIndex()", and the A_index value wasn't incrementing either.
I think you are misunderstanding. I would be interested to see some actual code. Descriptions of code only tell us what you think is happening. Loop, % patterns.MaxIndex() is (basically) just a less-elegant way or writing a for-loop.

Code: Select all

patterns := []
patterns[1] := [ [-2,0], [-1,0], [1, 0], [2, 0], [0,-1], [0,-2], [0, 1] ]
patterns[2] := [ [0,-2], [0,-1], [0,-1], [0,-2], [-1,0], [-2,0], [1, 0] ]
patterns[3] := [ [-2,0], [-1,0], [1, 0], [2, 0], [0, 1], [0, 2], [0,-1] ]
patterns[4] := [ [0, 1], [0, 2], [0,-1], [0,-2], [1, 0], [2, 0], [-1,0] ]
patterns[5] := [ [-2,0], [-1,0], [1, 0], [2, 0], [0,-1], [0,-2] ]
patterns[6] := [ [0,-2], [0,-1], [0,-1], [0,-2], [-1,0], [-2,0] ]
patterns[7] := [ [-2,0], [-1,0], [1, 0], [2, 0], [0, 1], [0, 2] ]
patterns[8] := [ [0, 1], [0, 2], [0,-1], [0,-2], [1, 0], [2, 0] ]
Loop, % patterns.MaxIndex()
{
	PatternNumber := A_Index
	Loop, % patterns[A_Index].MaxIndex()
	{
		MsgBox, % "PatternNumber: "	PatternNumber 						"`n"
				. "OffsetNumber: "  A_Index								"`n"
				. "Offset 1: "		patterns[PatternNumber, A_Index, 1]	"`n"
				. "Offset 2: "		patterns[PatternNumber, A_Index, 2]
	}
}
Entropy42
Posts: 29
Joined: 11 Dec 2016, 12:34

Re: For Loop on a sequentially numbered array

18 Jan 2017, 21:05

It's always possible that I'm misunderstanding something, but I don't know what to post. I was debugging this, and couldn't get the loop counter to increment using "for" or "loop". I went to bed, opened it up on a different computer in the morning, started debugging again, and it was working fine with either loop type. So I don't have a non-working version to post and ask for help on.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: peter_ahk and 322 guests