Cant Get Array To Fill In Empty Arrays After Displacing The Array Using InsertAt

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
roonyrooxcess
Posts: 61
Joined: 20 Nov 2020, 21:29

Cant Get Array To Fill In Empty Arrays After Displacing The Array Using InsertAt

07 May 2021, 08:48

Hi, Im trying to displace an array by 1 & fill in the empty arrays after its been displaced, using MyArray.InsertAt

The array displaces fine, but cant fill in the empty arrays after its been displaced, the displaced arrays still show 0 ...

Thanks.,

Code: Select all

#SingleInstance, Force

;Create Initial Array
Data := Object()
i:=109

NumberOfUnits:=10

Loop %NumberOfUnits%
{
	i++
	
	data[A_Index]:=i
}
Value := data[5]



	;Displays Created Cascade In Gui
		x:=0
		number:=0
		YAxisInc:=12
		StatusInc:=1
		NumStatusInc:=1
		First:=!First++
		Loop, %NumberOfUnits%
		{
			x++			;Increments by + to create 40 to 60
			y1:=data[x]
			YAxis:=number += YAxisInc ;Increments by 12 to create a gap of 12 between numbers
			
			Gui, Font, s8

						
				Gui,Add,Text,x360 w500  y%YAxis% vMainUpdate%x%,    %y1%   
				

		}


				Gui, Show,  x1609 y36 w900 h1200, XGraph





loop
{

									;Creates New Array To Cascade
									MainupDatex := Object()
									
									c:=0
									loop, 10
												{
									c++
												MainupDatex[c] := data[c]
												}
									c:=0
									





FillEmptyCells:=1

;Cascades Array By EveryTime OuterLoop 

;OuterLoop
Loop, 10
{



;Cascades Array By 1
MainupDatex.InsertAt(1, 0)

;Fill Empty Cells After Cascade By 1
MainupDatex[FillEmptyCells] := data[FillEmptyCells]

FillEmptyCells++


;Redisplays Cascaded Array
;InnerLoop
Loop,10
{


c++
cx++
			MainArrayToCopy:=MainupDatex[c]
			GuiControl,,          MainUpdate%cx% ,  %MainArrayToCopy%   
}
cx:=0
c:=0
Sleep 1000


}

FillEmptyCells:=1

}









esc::
{
send {esc}
sleep 100
Send {LControl down}
sleep 100
Send {s}
sleep 100
Send {LControl up}

Reload
}
Return
User avatar
boiler
Posts: 16772
Joined: 21 Dec 2014, 02:44

Re: Cant Get Array To Fill In Empty Arrays After Displacing The Array Using InsertAt

07 May 2021, 09:29

Put whatever you want to "fill" it with in place of the 0:

Code: Select all

MainupDatex.InsertAt(1, 0)
Example:

Code: Select all

MainupDatex.InsertAt(1, "hello")
roonyrooxcess
Posts: 61
Joined: 20 Nov 2020, 21:29

Re: Cant Get Array To Fill In Empty Arrays After Displacing The Array Using InsertAt

07 May 2021, 09:45

Hi, Doesnt seem to work, the 0's are replaced, but the entire array vanishes from the gui, I need it to display a constant column of numbers... if the array1 gets displaced to array2, i need to display a value from another array in array1's position

Thanks.,

Code: Select all

#SingleInstance, Force

;Create Initial Array
Data := Object()
i:=109

NumberOfUnits:=10

Loop %NumberOfUnits%
{
	i++
	
	data[A_Index]:=i
}
Value := data[5]



	;Displays Created Cascade In Gui
		x:=0
		number:=0
		YAxisInc:=12
		StatusInc:=1
		NumStatusInc:=1
		First:=!First++
		Loop, %NumberOfUnits%
		{
			x++			;Increments by + to create 40 to 60
			y1:=data[x]
			YAxis:=number += YAxisInc ;Increments by 12 to create a gap of 12 between numbers
			
			Gui, Font, s8

						
				Gui,Add,Text,x360 w500  y%YAxis% vMainUpdate%x%,    %y1%   
				

		}


				Gui, Show,  x1609 y36 w900 h1200, XGraph


;Creates New Array To Cascade
									MainupDatex := Object()
									
									c:=0
									loop, 10
												{
									c++
												MainupDatex[c] := data[c]
												}
									c:=0
									


loop
{

									





FillEmptyCells:=1

;Cascades Array By EveryTime OuterLoop 

;OuterLoop
Loop, 10
{



;Cascades Array By 1

Fillx := data[c]

MainupDatex.InsertAt(1, Fillx)




;Redisplays Cascaded Array
;InnerLoop
Loop,10
{


c++
cx++
			MainArrayToCopy:=MainupDatex[c]

			GuiControl,,          MainUpdate%cx% ,  %MainArrayToCopy%
sleep 100



}
cx:=0
c:=0
;Sleep 1000

c:=0
									
}
FillEmptyCells:=1

}









esc::
{
send {esc}
sleep 100
Send {LControl down}
sleep 100
Send {s}
sleep 100
Send {LControl up}

Reload
}
Return
User avatar
boiler
Posts: 16772
Joined: 21 Dec 2014, 02:44

Re: Cant Get Array To Fill In Empty Arrays After Displacing The Array Using InsertAt

07 May 2021, 09:48

I don't know what you are trying to do when you are "displacing" an array. I was just trying to help with the mechanics of using AHK objects. Perhaps someone else will study your code and help you debug it.
roonyrooxcess
Posts: 61
Joined: 20 Nov 2020, 21:29

Re: Cant Get Array To Fill In Empty Arrays After Displacing The Array Using InsertAt

07 May 2021, 09:52

I'm just moving the entire array up by 1, so all the numbers in array1 to 10, move to array2, array3 etc., I just cant figure out how to fill in the blank arrays it leaves behind, when it gets moved to array2, array3 etc., .. Hope that makes sense
User avatar
boiler
Posts: 16772
Joined: 21 Dec 2014, 02:44

Re: Cant Get Array To Fill In Empty Arrays After Displacing The Array Using InsertAt

07 May 2021, 10:03

I'm not 100% sure what you are trying to accomplish, but it seems like you are trying to recreate array operations that are better accomplished using built-in object methods, similar to using .InsertAt() instead of moving them all with loops in your code. To create a new array, use .Clone() and then you can insert whatever you want in either array. I'm not understanding what you mean by blank arrays left behind and why that would need to happen.

Example using .Clone():

Code: Select all

Array1 := [1, 2, 3, 4, 5]
Array2 := Array1.Clone()
Array2.InsertAt(1, "hello")
DisplayArray(Array1)
DisplayArray(Array2)
return

DisplayArray(a) {
	for k, v in a
		out .= k "`t" v "`n"
	MsgBox, % out
}
roonyrooxcess
Posts: 61
Joined: 20 Nov 2020, 21:29

Re: Cant Get Array To Fill In Empty Arrays After Displacing The Array Using InsertAt

07 May 2021, 10:15

Could you turn that into a continous loop ...

So if hello is in array2, the number 1 is displayed above the hello, in array1, & the numbers 1 to 5 move to array 3 below hello
If hello is in array3 the numbers 1 & 2 are above the hello, in arrays 1 & 2. & the numbers 1 to 5 move to array 4 below hello
If hello is in array4 the numbers 1 & 2 & 3 are above the hello, in arrays 1 & 2 & 3. etc.,

That's basically what im trying to do ...

Thanks.,
User avatar
boiler
Posts: 16772
Joined: 21 Dec 2014, 02:44

Re: Cant Get Array To Fill In Empty Arrays After Displacing The Array Using InsertAt

07 May 2021, 11:01

I cannot follow what you're saying at all. There is a lot of ambiguity as to what the starting points are, what gets moved via the script actions, etc. Can you show what the starting point is and what you want the script to do in terms of something like below? I showed how I tried to interpret what you said in the comments and indicate where it's unclear/ambiguous.

Code: Select all

; starting point:
; is only Array1 the starting point, or is it Array1 and Array2, and Arrays 3 and beyond are created from both of them?
Array1 := [1, "hello", 2, 3, 4, 5] ; "the number 1 is displayed above the hello in array1" (but are 2 through 5 supposed to be in it? where?)
Array2 := ["hello"] ; "hello is in array2" (and nothing else?)

; desired result via script actions:
Array3 := ["hello", 1, 2, 3, 4, 5] ; "the numbers 1 to 5 move to array 3 below hello" (move from which array?)
User avatar
Chunjee
Posts: 1402
Joined: 18 Apr 2014, 19:05
Contact:

Re: Cant Get Array To Fill In Empty Arrays After Displacing The Array Using InsertAt

07 May 2021, 11:12

I think it might be useful to give an example output of what you want.

I ran the script and saw data was an array like this: [110, 111, 112, 113, 114, 115, 116, 117, 118, 119]

What do you want your new array to be?


fyi this kind of indentation is not readable:

Code: Select all

loop
{
									;Creates New Array To Cascade
									MainupDatex := Object()
									
									c:=0
									loop, 10
												{
									c++
												MainupDatex[c] := data[c]
												}
									c:=0
									

User avatar
boiler
Posts: 16772
Joined: 21 Dec 2014, 02:44

Re: Cant Get Array To Fill In Empty Arrays After Displacing The Array Using InsertAt

07 May 2021, 11:16

Chunjee wrote: fyi this kind of indentation is not readable
Exactly. It's difficult to look at visually as well as follow what it's trying to accomplish, which is why I'm referring to short examples instead of details in the code.
roonyrooxcess
Posts: 61
Joined: 20 Nov 2020, 21:29

Re: Cant Get Array To Fill In Empty Arrays After Displacing The Array Using InsertAt

07 May 2021, 11:29

Ok, I've put things in step by step, this should do the job

Array1 := [1, 2, 3, 4, 5] ;

Array2 := [a, b, c, d, e] ;

Array3 := [Position1, Position2, Position3, Position4, Position5] ;

Step 1. Copy All numbers in Array1 to Position2 to Position5 in Array3, Now that Position1 is Empty, Copy a from Array2 to Position1 in Array3

Step 3. Copy All numbers in Array1 to Position3 to Position5 in Array3

Step 4. Then Copy b from Array2 to Position2 in Array3

Step 5. Copy All numbers in Array1 to Position4 to Position5 in Array3

Step 6. Copy c from Array2 to Position3 in Array3

Step 7. Copy All numbers in Array1 to Position5 to Position5 in Array3

Step 8. Then Copy d from Array2 to Position4 in Array3

Step 9. Copy All numbers in Array1 to Position5 to Position5 in Array3

Step 10. Copy e from Array2 to Position5 in Array3
roonyrooxcess
Posts: 61
Joined: 20 Nov 2020, 21:29

Re: Cant Get Array To Fill In Empty Arrays After Displacing The Array Using InsertAt

07 May 2021, 11:39

Forgot to put examples ... this should do the job ...

Array1 := [1, 2, 3, 4, 5] ;

Array2 := [a, b, c, d, e] ;

Array3 := [Position1, Position2, Position3, Position4, Position5] ;

Step 1. Copy All numbers in Array1 to Position2 to Position5 in Array3, Now that Position1 is Empty, Copy a from Array2 to Position1 in Array3
Array3 := [a, 1, 2, 3, 4] ;

Step 3. Copy All numbers in Array1 to Position3 to Position5 in Array3
Step 4. Then Copy b from Array2 to Position2 in Array3

Array3 := [a, b,1, 2, 3] ;

Step 5. Copy All numbers in Array1 to Position4 to Position5 in Array3
Step 6. Copy c from Array2 to Position3 in Array3

Array3 := [a, b,c,1, 2] ;

Step 7. Copy All numbers in Array1 to Position5 to Position5 in Array3
Step 8. Then Copy d from Array2 to Position4 in Array3

Array3 := [a, b,c,d,1 ] ;

Step 9. Copy All numbers in Array1 to Position5 to Position5 in Array3
Step 10. Copy e from Array2 to Position5 in Array3

Array3 := [a, b,c,d,e ] ;
User avatar
boiler
Posts: 16772
Joined: 21 Dec 2014, 02:44

Re: Cant Get Array To Fill In Empty Arrays After Displacing The Array Using InsertAt

07 May 2021, 12:45

Code: Select all

Array1 := [1, 2, 3, 4, 5]
Array2 := ["a", "b", "c", "d", "e"]
Array3 := Array1.Clone()
DisplayArray(Array3)

loop, % Array1.Count() {
	Array3.InsertAt(A_Index, Array2[A_Index])
	Array3.Pop()
	DisplayArray(Array3)
}
return

DisplayArray(a) {
	for k, v in a
		out .= v " "
	MsgBox, % out
}

Edit: Corrected .Pop() to remove key reference as it removes the last item and requires no parameter.
Last edited by boiler on 07 May 2021, 13:57, edited 1 time in total.
roonyrooxcess
Posts: 61
Joined: 20 Nov 2020, 21:29

Re: Cant Get Array To Fill In Empty Arrays After Displacing The Array Using InsertAt

07 May 2021, 13:05

Thats great thanks! Apologies for not including examples, been a while since i used this forum.

Thanks again ...
User avatar
Chunjee
Posts: 1402
Joined: 18 Apr 2014, 19:05
Contact:

Re: Cant Get Array To Fill In Empty Arrays After Displacing The Array Using InsertAt

07 May 2021, 13:44

roonyrooxcess wrote:
07 May 2021, 11:39
Copy All numbers in Array1 to Position4 to Position5 in Array3
Not sure why these steps are jumping all over the map. 1,2,3,4,5 seems more straighforward.

Sounds like it all worked out in the end though. :thumbup:
roonyrooxcess
Posts: 61
Joined: 20 Nov 2020, 21:29

Re: Cant Get Array To Fill In Empty Arrays After Displacing The Array Using InsertAt

07 May 2021, 13:53

Hi, slight hitch ... I'm trying to incorporate this in a gui, but cant figure out how to get it to work using guicontrol ...

Code: Select all

#SingleInstance, Force

;Create Initial Array
Array1  := Object()
Array2  := Object()
Array3  := Object()
i:=109

NumberOfUnits:=10

Loop %NumberOfUnits%
{
	i++
	Array1[A_Index]:=i
}

i:=209
Loop %NumberOfUnits%
{
	i++	
	Array2[A_Index]:=i
}

i:=0
Loop %NumberOfUnits%
{
	i++	
	Array3[A_Index]:=0
}










	;Displays Created Cascade In Gui
		x:=0
		number:=0
		YAxisInc:=12
		
		Loop, %NumberOfUnits%
		{
			x++			;Increments by + to create 40 to 60
			y1:=Array1[x]
			y2:=Array2[x]
			y3:=Array3[x]

			YAxis:=number += YAxisInc ;Increments by 12 to create a gap of 12 between numbers
			
			Gui, Font, s8

						
				Gui,Add,Text,x360   y%YAxis% vArray1Update%x%,     %y1% 
				Gui,Add,Text,x560   y%YAxis% vArray2Update2%x%,    %y2%
				Gui,Add,Text,x460 w20  y%YAxis% vArray3Update3%x%, %y3%   

		}


				Gui, Show,  x1609 y36 w900 h1200, XGraph



						



						Array3 := Array1.Clone()



						Loop
						
						{
						
						Data2Inc:=1
						IncBy2:=1
						
						loop, % Array1.Count() {
							Array3.InsertAt(A_Index, Array2[A_Index])
							Array3.Pop(10)					
						
						CopyFromArray:=Array3[Data2Inc]
						
						GuiControl,,   Array3Update3%IncBy2%, % CopyFromArray

						
						Data2Inc++
						IncBy2++	
						sleep 500
						
						
						} 
						
						} 
						Return


esc::
{

sleep 100
Send {LControl down}
sleep 100
Send {s}
sleep 100
Send {LControl up}

Reload
}
Return
User avatar
boiler
Posts: 16772
Joined: 21 Dec 2014, 02:44

Re: Cant Get Array To Fill In Empty Arrays After Displacing The Array Using InsertAt

07 May 2021, 14:19

I'm guessing the value of CopyFromArray doesn't contain what you think it should contain because it looks like it's updating the controls but after a point the value is the same as what's already there as the ToolTip I added indicates.

I also changed the indenting and removed extra blank lines to make it easier to review. And I removed the parameter from .Pop() as I did in my code above. It just removes the last item and requires no parameter. I had accidentally left the parameter in when I originally used .RemoveAt() instead.

Code: Select all

#SingleInstance, Force

;Create Initial Array
Array1  := Object()
Array2  := Object()
Array3  := Object()
i:=109

NumberOfUnits:=10

Loop %NumberOfUnits%
{
	i++
	Array1[A_Index]:=i
}

i:=209
Loop %NumberOfUnits%
{
	i++	
	Array2[A_Index]:=i
}

i:=0
Loop %NumberOfUnits%
{
	i++	
	Array3[A_Index]:=0
}

;Displays Created Cascade In Gui
x:=0
number:=0
YAxisInc:=12

Loop, %NumberOfUnits%
{
	x++			;Increments by + to create 40 to 60
	y1:=Array1[x]
	y2:=Array2[x]
	y3:=Array3[x]

	YAxis:=number += YAxisInc ;Increments by 12 to create a gap of 12 between numbers
	
	Gui, Font, s8

	Gui,Add,Text,x360   y%YAxis% vArray1Update%x%,     %y1% 
	Gui,Add,Text,x560   y%YAxis% vArray2Update2%x%,    %y2%
	Gui,Add,Text,x460 w20  y%YAxis% vArray3Update3%x%, %y3%   
}

Gui, Show,  x1609 y36 w900 h1200, XGraph

Array3 := Array1.Clone()

Loop
{
	Data2Inc:=1
	IncBy2:=1

	loop, % Array1.Count() {
		Array3.InsertAt(A_Index, Array2[A_Index])
		Array3.Pop()					

		CopyFromArray:=Array3[Data2Inc]
		ToolTip, % "Updating Array3Update3" IncBy2 " to value " CopyFromArray, 600, 80
		GuiControl,,   Array3Update3%IncBy2%, % CopyFromArray

		Data2Inc++
		IncBy2++	
		sleep 500
	} 
} 
Return


esc::
	sleep 100
	Send {LControl down}
	sleep 100
	Send {s}
	sleep 100
	Send {LControl up}
	Reload
Return
roonyrooxcess
Posts: 61
Joined: 20 Nov 2020, 21:29

Re: Cant Get Array To Fill In Empty Arrays After Displacing The Array Using InsertAt

07 May 2021, 14:21

Thats great, thanks! Trying out the script, much appreciated!
roonyrooxcess
Posts: 61
Joined: 20 Nov 2020, 21:29

Re: Cant Get Array To Fill In Empty Arrays After Displacing The Array Using InsertAt

07 May 2021, 15:49

I got the script to do basically what I wanted, its still a bit flaky, as it deletes the entire column of numbers, once it reaches 20, instead of keeping the previous numbers it displayed ... apart from that it works great

Code: Select all

#SingleInstance, Force

;Create Initial Array
Data := Object()
i:=0

NumberOfUnits:=90

Loop %NumberOfUnits%
{
	i++

	xs=%i%
	
	data[A_Index]:=xs
}
Value := data[5]



	;Displays Created Cascade In Gui
		x:=0
		number:=0
		YAxisInc:=12
		StatusInc:=1
		NumStatusInc:=1
		First:=!First++
		Loop, 20
		{
			x++			;Increments by + to create 40 to 60
			y1:=data[x]
			YAxis:=number += YAxisInc ;Increments by 12 to create a gap of 12 between numbers
			
			Gui, Font, s8

						
				Gui,Add,Text,x360 w500  y%YAxis% vMainUpdate%x%,    %y1%   
				

		}


				Gui, Show,  x1609 y36 w900 h1200, XGraph


;Creates New Array To Cascade 
									MainupDatex := Object()
									
									c:=0
									loop, 90
												{
									c++
												MainupDatex[c] := data[c]
												}
									
									

c:=1
loop
{

									





FillEmptyCells:=1

;Cascades Array By EveryTime OuterLoop 

;OuterLoop
Loop, 20
{



;Cascades Array By 1 Using InsertAt



Fillx := data[c]

a=apple

MainupDatex.InsertAt(1, Fillx)

Update++

MainArrayToCopyx:=Data[Update]
GuiControl,,          MainUpdate1 ,  %MainArrayToCopyx%


sleep 500
cx:=1
c:=0
;Redisplays Cascaded Array
;InnerLoop


Loop,20
{


c++
cx++
			MainArrayToCopy:=MainupDatex[c]

			GuiControl,,          MainUpdate%cx% ,  %MainArrayToCopy%



}


;Fills In Gaps Created By Cascading Array 
c:=0
StartFromUpDateInDisplaceArray:=Update
FillinGapPosition:=2

Loop, %Update%
{


FillinGaps := data[StartFromUpDateInDisplaceArray]


GuiControl,,          MainUpdate%FillinGapPosition% ,  %FillinGaps%

StartFromUpDateInDisplaceArray++
FillinGapPosition++
}

cx:=1
c:=0
;Sleep 1000

c:=0
									
}



FillEmptyCells:=1
;Resets
									;MainupDatex := Object()
									;
									;c:=0
									;loop, 20
									;			{
									;c++
									;			MainupDatex[c] := data[c]
									;			}
									;c:=0


Update:=0
}









esc::
{
send {esc}
sleep 100
Send {LControl down}
sleep 100
Send {s}
sleep 100
Send {LControl up}

Reload
}
Return
User avatar
boiler
Posts: 16772
Joined: 21 Dec 2014, 02:44

Re: Cant Get Array To Fill In Empty Arrays After Displacing The Array Using InsertAt

07 May 2021, 20:03

Try using a debugger to keep track of what the contents of the variables are at each step. If you've never used one and aren't looking to start now, then add ToolTips or MsgBoxes so you can report to yourself what is happening at each step so you can see where it's going wrong and why.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 190 guests