Trouble with Basics, pick 3 Elements out of 5

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
iMMOLA
Posts: 8
Joined: 17 Nov 2020, 05:31

Trouble with Basics, pick 3 Elements out of 5

25 Nov 2020, 04:20

Hi there guys, so I'm a noob when it comes to coding and I'm having troubles making my script work.
What I want to do is pick n elements out of m without picking the same element twice. Each element has a specific unique position and when an element is picked, the mouse should move to the position.
However, my script isn't doing anything. Here is what I came up with

Code: Select all

random, l, 1, 5

obj := {1: "x = 1110, y = 588", 2: "x = 1110, y = 669", 3: "x = 1110, y = 750", 4: "x = 1110, y = 830", 5: "x = 1110, y = 910"}	; do I even need to specify x & y here or would 1: "1110, 588" also work? I probably also need to create a connection between the random number picked and the objecties?

MouseMove, x%x%, y%y% obj["l"]		 ; do I even need to specify x & y here or would "MouseMove, % obj["l]" also work? (like I did below)

if((m := rand(1, 5)) != l)

MouseMove, % obj["m"]

if((n := rand(1, 5)) != l | m)

MouseMove, % obj["m"]

; then finish and pick another 3 elements

; I also tried 
random, var_1, 1, 5

l := %var_1%
but that didn't work either. What am I doing wrong?

ok so I learned that I have to use stringsplit for this to work, however, my script still isn't doing anything

Code: Select all


random, l, 1, 5

obj := {1: "1110,588", 2: "1110,669", 3: "1110,750", 4: "1110,830", 5: "1110,910"}

var_1 := obj["%l%"]

stringsplit coordinatesxy, var_1, `,

MouseMove %coordinatesxy1%, %coordinatesxy2%


best regards
Last edited by iMMOLA on 25 Nov 2020, 05:27, edited 2 times in total.
mcl
Posts: 357
Joined: 04 May 2018, 16:35

Re: Trouble with Basics, pick 3 Elements out of 5

25 Nov 2020, 05:27

MouseMove takes numbers as arguments. In your code first argument is x, which is treated as string, and then variable x, which is undefined.
If you put both coords into one string, like "1110,588", it will be treated as a single argument even after %expansion%, thus won't work either.

I suggest you rewrite your obj to be an array containing objects instead of strings:

Code: Select all

obj := [ {x:1110,y:588}, {x:1110,y:669}, ...]
mx := obj[L].x
my := obj[L].y
MouseMove, %mx%, %my%
Next, there is no function rand, so your if's won't work.
And even if they do, you can get only one MouseMove action if all random numbers happen to be the same.
However, you can just remove used elements from obj:

Code: Select all

...
MouseMove, %mx%, %my%
obj.RemoveAt(L)
So, here is my complete code suggestion:

Code: Select all

obj := [ {x:1110,y:588}, {x:1110,y:669}, {x:1110,y:750}, {x:1110,y:830}, {x:1110,y:910} ]

Random, L, 1, 5
mx := obj[L].x
my := obj[L].y
MouseMove, %mx%, %my%

obj.RemoveAt(L)  ; Remove used element

Random, L, 1, 4  ; Now only four elements left in obj
mx := obj[L].x
my := obj[L].y
MouseMove, %mx%, %my%

obj.RemoveAt(L)  ; Remove used element

Random, L, 1, 3
mx := obj[L].x
my := obj[L].y
MouseMove, %mx%, %my%
Also I must point out that mouse will move instantly and no clicks will be made. Are you sure this is what you need?
github://oGDIp - GDI+ wrapper for AHK v1.1
iMMOLA
Posts: 8
Joined: 17 Nov 2020, 05:31

Re: Trouble with Basics, pick 3 Elements out of 5

25 Nov 2020, 05:31

mcl wrote:
25 Nov 2020, 05:27
MouseMove takes numbers as arguments. In your code first argument is x, which is treated as string, and then variable x, which is undefined.
If you put both coords into one string, like "1110,588", it will be treated as a single argument even after %expansion%, thus won't work either.

I suggest you rewrite your obj to be an array containing objects instead of strings:

Code: Select all

obj := [ {x:1110,y:588}, {x:1110,y:669}, ...]
mx := obj[L].x
my := obj[L].y
MouseMove, %mx%, %my%
Next, there is no function rand, so your if's won't work.
And even if they do, you can get only one MouseMove action if all random numbers happen to be the same.
However, you can just remove used elements from obj:

Code: Select all

...
MouseMove, %mx%, %my%
obj.RemoveAt(L)
So, here is my complete code suggestion:

Code: Select all

obj := [ {x:1110,y:588}, {x:1110,y:669}, {x:1110,y:750}, {x:1110,y:830}, {x:1110,y:910} ]

Random, L, 1, 5
mx := obj[L].x
my := obj[L].y
MouseMove, %mx%, %my%

obj.RemoveAt(L)  ; Remove used element

Random, L, 1, 4  ; Now only four elements left in obj
mx := obj[L].x
my := obj[L].y
MouseMove, %mx%, %my%

obj.RemoveAt(L)  ; Remove used element

Random, L, 1, 3
mx := obj[L].x
my := obj[L].y
MouseMove, %mx%, %my%
Also I must point out that mouse will move instantly and no clicks will be made. Are you sure this is what you need?
thank you very much for you reply, this solution looks much better than mine. Yes, I know I'll have to add sleeps and clicks for the script to do what I want, I'll try that now :) I also edited my post because I found the stringsplit function but like I said, your solution seems to do the trick thank you very much!
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Trouble with Basics, pick 3 Elements out of 5

25 Nov 2020, 05:47

Code: Select all

obj := [588,669,750,830,910]
F12::						; press F12 for testing.
	Random, no, 1,% obj.Length()		
	MsgBox % "1100 " obj[no] "`n" obj.Length()
;	MouseMove, 1100, obj[no]
	obj.Length() != 3 ? obj.RemoveAt(no) : (obj := [588,669,750,830,910])
	Return
Last edited by BoBo on 26 Nov 2020, 06:05, edited 1 time in total.
Reason: Edit: removed 'if' from ternary-operator as advised by Boiler below! Thx for the reminder.
iMMOLA
Posts: 8
Joined: 17 Nov 2020, 05:31

Re: Trouble with Basics, pick 3 Elements out of 5

25 Nov 2020, 07:01

BoBo wrote:
25 Nov 2020, 05:47

Code: Select all

obj := [588,669,750,830,910]
F12::						; press F12 for testing.
	Random, no, 1,% obj.Length()		
	MsgBox % "1100 " obj[no] "`n" obj.Length()
;	MouseMove, 1100, obj[no]
	if obj.Length() != 3 ? obj.RemoveAt(no) : (obj := [588,669,750,830,910])
	Return
this code only return the x position tho if I'm not mistaken? Thank your for your answer tho, very much appreciated.

I working with mcls code now, but I have another question. I want to create as many entities consisting of 5 elements as possible. Since my array is going to have 60 objects and the binomial coefficient of 60 over 5 is insanely large it's ok to put the objects back in the array. Now my question is: how do I reset the array after one loop of picking 5 objects is finished?

Also I'm getting the error message "expression is too long" because like I said, my array has 60 objects. Is there a way to bypass this limit for example by using two arrays but still retain the original function?
I've also read that it's possible to simply put the objects, or in my case coordinate pairs, into a text file and tell ahk to retrieve the objects from there?
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Trouble with Basics, pick 3 Elements out of 5

25 Nov 2020, 07:11

this code only return the x position tho if I'm not mistaken?
Nope, it returns a randomized y-coord what makes sense to me as your x-coord is a static '1110' for all x/y-coords you've provided (what btw, shrinks the object's length, hence it's getting less endangered to end up with an 'expression too long' error)
Now my question is: how do I reset the array after one loop of picking 5 objects is finished?
That's exactly what my code does, it resets the whole array after the 3 (requested) attempts.
iMMOLA
Posts: 8
Joined: 17 Nov 2020, 05:31

Re: Trouble with Basics, pick 3 Elements out of 5

25 Nov 2020, 07:48

BoBo wrote:
25 Nov 2020, 07:11
this code only return the x position tho if I'm not mistaken?
Nope, it returns a randomized y-coord what makes sense to me as your x-coord is a static '1110' for all x/y-coords you've provided (what btw, shrinks the object's length, hence it's getting less endangered to end up with an 'expression too long' error)
Now my question is: how do I reset the array after one loop of picking 5 objects is finished?
That's exactly what my code does, it resets the whole array after the 3 (requested) attempts.
Oh, that was my bad, sorry. I only provided the first object coordinates, the x values also differ its a 5x12 array. I also just understood that your code resets the array, thank you very much! :)
User avatar
boiler
Posts: 17073
Joined: 21 Dec 2014, 02:44

Re: Trouble with Basics, pick 3 Elements out of 5

25 Nov 2020, 13:45

BoBo wrote:
25 Nov 2020, 05:47

Code: Select all

obj := [588,669,750,830,910]
F12::						; press F12 for testing.
	Random, no, 1,% obj.Length()		
	MsgBox % "1100 " obj[no] "`n" obj.Length()
;	MouseMove, 1100, obj[no]
	if obj.Length() != 3 ? obj.RemoveAt(no) : (obj := [588,669,750,830,910])
	Return
Remember that combining if with a ternary statement isn’t doing what you think it does:
https://www.autohotkey.com/boards/viewtopic.php?p=349388#p349394

The ternary operator has the if built in. When you add an if before it, you are making the line following it (the return in this case), conditional on the result above it. And the result isn’t even the condition itself. It’s whether resulting outcome of the two outcomes is considered “truthy” or not. Definitely not what you are looking to do here. I’m sure you don’t want the return to be conditional under any circumstances, and that’s only one issue with it.
iMMOLA
Posts: 8
Joined: 17 Nov 2020, 05:31

Re: Trouble with Basics, pick 3 Elements out of 5

25 Nov 2020, 14:07

Code: Select all

a::

obj := [ {x:1118,y:590}, {x:1119,y:640}, {x:1116,y:680}, {x:1119,y:735}, {x:1116,y:774}, {x:1170,y:591}, {x:1166,y:637}, {x:1166,y:687}, {x:1167,y:781}, {x:1166,y:783},  {x:1218,y:593}, {x:1213,y:639}, {x:1212,y:688}, {x:1215,y:741}, {x:1216,y:780}, {x:1260,y:590}, {x:1262,y:637}, {x:1263,y:687}, {x:1261,y:733}, {x:1264,y:784}, {x:1311,y:591}, {x:1309,y:639}, {x:1311,y:690}, {x:1311,y:734}, {x:1313,y:783}, {x:1358,y:591}, {x:1360,y:638}, {x:1359,y:688}, {x:1359,y:735}, {x:1359,y:782}, {x:1408,y:590}, {x:1405,y:638}, {x:1404,y:686}, {x:1406,y:733}, {x:1405,y:782}, {x:1454,y:588}, {x:1451,y:638}, {x:1453,y:687}, {x:1454,y:735}, {x:1455,y:782}, {x:1503,y:587}, {x:1502,y:637}, {x:1503,y:687}, {x:1500,y:737} {x:1501,y:783}, {x:1550,y:588}, {x:1552,y:638}, {x:1549,y:686}, {x:1552,y:734}, {x:1551,y:783}, {x:1596,y:587}, {x:1597,y:634}, {x:1598,y:684}, {x:1598,y:734}, {x:1600,y:787}, {x:1649,y:589}, {x:1645,y:640}, {x:1647,y:687}, {x:1645,y:734}, {x:1649,y:782} ]

Random, L, 1, % obj.Length()
mx := obj[L].x
my := obj[L].y
MouseMove, %mx%, %my%
Sleep 200
Send ^{Click}
Sleep 50


obj.RemoveAt(L)  ; Remove used element

Random, L, 1, % obj.Length()  ; Now only 59 elements left in obj
mx := obj[L].x
my := obj[L].y
MouseMove, %mx%, %my%
Sleep 240
Send ^{Click}
Sleep 50

obj.RemoveAt(L)  ; Remove used element

Random, L, 1, % obj.Length()
mx := obj[L].x
my := obj[L].y
MouseMove, %mx%, %my%
Sleep 240
Send ^{Click}
Sleep 50

obj.RemoveAt(L)  ; Remove used element

Random, L, 1, % obj.Length()
mx := obj[L].x
my := obj[L].y
MouseMove, %mx%, %my%
Sleep 240
Send ^{Click}
Sleep 50

obj.RemoveAt(L)  ; Remove used element

Random, L, 1, % obj.Length()
mx := obj[L].x
my := obj[L].y
MouseMove, %mx%, %my%
Sleep 240
Send ^{Click}
Sleep 50

if obj.Length() = 55 ? : (obj := [ {x:1118,y:590}, {x:1119,y:640}, {x:1116,y:680}, {x:1119,y:735}, {x:1116,y:774}, {x:1170,y:591}, {x:1166,y:637}, {x:1166,y:687}, {x:1167,y:781}, {x:1166,y:783}, {x:1218,y:593}, {x:1213,y:639}, {x:1212,y:688}, {x:1215,y:741}, {x:1216,y:780}, {x:1260,y:590}, {x:1262,y:637}, {x:1263,y:687}, {x:1261,y:733}, {x:1264,y:784}, {x:1311,y:591}, {x:1309,y:639}, {x:1311,y:690}, {x:1311,y:734}, {x:1313,y:783}, {x:1358,y:591}, {x:1360,y:638}, {x:1359,y:688}, {x:1359,y:735}, {x:1359,y:782}, {x:1408,y:590}, {x:1405,y:638}, {x:1404,y:686}, {x:1406,y:733}, {x:1405,y:782}, {x:1454,y:588}, {x:1451,y:638}, {x:1453,y:687}, {x:1454,y:735}, {x:1455,y:782}, {x:1503,y:587}, {x:1502,y:637}, {x:1503,y:687}, {x:1500,y:737} {x:1501,y:783}, {x:1550,y:588}, {x:1552,y:638}, {x:1549,y:686}, {x:1552,y:734}, {x:1551,y:783}, {x:1596,y:587}, {x:1597,y:634}, {x:1598,y:684}, {x:1598,y:734}, {x:1600,y:787}, {x:1649,y:589}, {x:1645,y:640}, {x:1647,y:687}, {x:1645,y:734}, {x:1649,y:782} ])

MouseMove, 276, 224
Send {Space Down}
Sleep 200
Send {Space Up}
Sleep 50

Send {Esc}
MouseMove, 846, 280
Sleep 100
Send {Click}

return

ok so this is what the code looks like right now

With this array I get the "expression too long" error tho and since I couldn't find a way to bypass this error I thought about creating the pairs using two arrays each consisting of only x and only y coordinates but I don't know if that's the way to go... I took all the coordinates manually, so there actually are only 17 different values.

Code: Select all

x: 1118
x: 1166
x: 1215
x: 1260
x: 1310
x: 1360
x: 1405
x: 1455
x: 1500
x: 1550
x: 1600
x: 1645

y: 590
y: 640
y: 680
y: 735
y: 775

Code: Select all

obj := XCoord( {x:1118},{x:1166},{x:1215},{x:1260},{x:1310},{x:1360},{x:1405},{x:1455},{x:1500},{x:1550},{x:1600},{x:1645} )
Random, L, 1, 12
mx := obj[L].x # I probably need to specify the array here, but I don't know how
obj := YCoord( {y:590},{y:640},{y:680},{y:735},{y:775} )
Random, M, 1, 5
my := obj[M].y # here again
MouseMove, %mx%, %my%
Sleep 200
Send ^{Click}
Sleep 50
However I then need to somehow tell the script to not take the exact same 2 coordinates and pair them up, so maybe something like this?
This would be the 2nd pair of coordinates

Code: Select all

obj := XCoord( {x:1118},{x:1166},{x:1215},{x:1260},{x:1310},{x:1360},{x:1405},{x:1455},{x:1500},{x:1550},{x:1600},{x:1645} )
obj := YCoord( {y:590},{y:640},{y:680},{y:735},{y:775} )

while((N := rand(1, 12)) != L AND O := rand(1, 5) =! M) # just for me to understand this: this means that if N doesn't equal L and O doesn't equal M the action of "picking" an item is performed, right?
mx := obj[N].x
my := obj[O].y
MouseMove, %mx%, %my%
Sleep 200
Send ^{Click}
Sleep 50

But how would I go on with the "while" expression for the 3rd, 4th & 5th pick. Is there an easier way to do this? I'm trying my best :D Thank you all again for your help!
Last edited by iMMOLA on 26 Nov 2020, 02:42, edited 1 time in total.
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Trouble with Basics, pick 3 Elements out of 5

25 Nov 2020, 15:30

@boiler Oops, I did it again! :oops:
@iMMOLA - your initial x-coord is 1118 (I'd round it to 1120) with all consecutive x-coords +50, so I'd guess that it isn't necessary to list all of them separately.
You could do some math instead ...

Code: Select all

F11::						; press F11 for testing
	Random, xPos, 1, 11		; which x-coord should be selected (in your sample you had 11 different ones)
	MsgBox % 1120+(xPos*50)	; do some math based on the initial/first x-coord that is 1118 (1120)
	Return
How to bundle a number of arrays to bypass the 'expression too long' msg.

Code: Select all

	obj1 := {1:[1118,588],2:[1166,669],3:[1215,750],4:[1260,830],5:[1310,910]}
	obj2 := {1:[1118,588],2:[1166,669],3:[1215,750],4:[1260,830],5:[1310,910]}
	obj3 := {1:[1118,588],2:[1166,669],3:[1215,750],4:[1260,830],5:[1310,910]}
	obj4 := {1:[1118,588],2:[1166,669],3:[1215,750],4:[1260,830],5:[1310,910]}
	obj5 := {1:[1118,588],2:[1166,669],3:[1215,750],4:[1260,830],5:[1310,910]}
	obj6 := {1:[1118,588],2:[1166,669],3:[1215,750],4:[1260,830],5:[1310,910]}
	
	obj	 := [obj1,obj2,obj3,obj4,obj5,obj6]			; array of associative arrays

	Random, oNo, 1,% obj.Length()					; which associative array to inspect
	Random, iNo, 1,% obj[oNo].Length()				; which item of the selected associative array to extract
	MsgBox % obj[oNo][iNo].1 " " obj[oNo][iNo].2	; etracted x + y coord
	Return
iMMOLA
Posts: 8
Joined: 17 Nov 2020, 05:31

Re: Trouble with Basics, pick 3 Elements out of 5

26 Nov 2020, 04:51

Okay so I tried to put all the elements into one array once again, but this doesn't work unfortunately.

Code: Select all

obj := { [12,59], [12,64], [12,68], [12,73], [12,77], [17,59], [17,64], [17,69], [17,78], [21,59], [21,64], [21,69], [21,74], [21,78], [26,59], [26,64], [26,69], [26,73], [26,78], [31,59], [31,64], [31,69], [31,73], [31,78], [36,59], [36,64], [36,69], [36,73], [36,78], [40,59], [40,64], [40,69], [40,73], [40,78], [45,59], [45,64], [45,69], [45,74], [45,78], [50,59], [50,64], [50,69], [50,74], [50,78], [55,59], [55,64], [55,69], [55,73], [55,78], [60,59], [60,63], [60,68], [60,73], [60,79], [65,59], [65,64], [65,69], [65,73], [65,78] }

Random, L, 1, % obj.Length()
mx := obj[L].1 * 10 + 1000
my := obj[L].2 * 10
MouseMove, %mx%, %my%
Sleep 240
Send ^{Click}
Sleep 50
So I guess I'll be working with 2 arrays. This is what I came up with using BoBos code.

Code: Select all


F12::

obj1 := [ {x:1118,y:590}, {x:1119,y:640}, {x:1116,y:680}, {x:1119,y:735}, {x:1116,y:774}, {x:1170,y:591}, {x:1166,y:637}, {x:1166,y:687}, {x:1166,y:735}, {x:1167,y:781}, {x:1218,y:593}, {x:1213,y:639}, {x:1212,y:688}, {x:1215,y:741}, {x:1216,y:780}, {x:1260,y:590}, {x:1262,y:637}, {x:1263,y:687}, {x:1261,y:733}, {x:1264,y:784}, {x:1311,y:591}, {x:1309,y:639}, {x:1311,y:690}, {x:1311,y:734}, {x:1313,y:783}, {x:1358,y:591}, {x:1360,y:638}, {x:1359,y:688}, {x:1359,y:735}, {x:1359,y:782} ]
obj2 := [ {x:1408,y:590}, {x:1405,y:638}, {x:1404,y:686}, {x:1406,y:733}, {x:1405,y:782}, {x:1454,y:588}, {x:1451,y:638}, {x:1453,y:687}, {x:1454,y:735}, {x:1455,y:782}, {x:1503,y:587}, {x:1502,y:637}, {x:1503,y:687}, {x:1500,y:737} {x:1501,y:783}, {x:1550,y:588}, {x:1552,y:638}, {x:1549,y:686}, {x:1552,y:734}, {x:1551,y:783}, {x:1596,y:587}, {x:1597,y:634}, {x:1598,y:684}, {x:1598,y:734}, {x:1600,y:787}, {x:1649,y:589}, {x:1645,y:640}, {x:1647,y:687}, {x:1645,y:734}, {x:1649,y:782} ]
obj := [obj1,obj2]

Random, oNo, 1,% obj.Length()
Random, iNo, 1,% obj[oNo].Length()
mx := obj[iNo].x
my := obj[iNo].y
MouseMove, %mx%, %my%
Sleep 200
Send ^{Click}
Sleep 50


obj.RemoveAt(iNo)  ; Remove used element

Random, oNo, 1,% obj.Length()
Random, iNo, 1,% obj[oNo].Length()
mx := obj[iNo].x
my := obj[iNo].y
MouseMove, %mx%, %my%
Sleep 200
Send ^{Click}
Sleep 50

obj.RemoveAt(iNo)  ; Remove used element

Random, oNo, 1,% obj.Length()
Random, iNo, 1,% obj[oNo].Length()
mx := obj[iNo].x
my := obj[iNo].y
MouseMove, %mx%, %my%
Sleep 200
Send ^{Click}
Sleep 50

obj.RemoveAt(iNo)  ; Remove used element

Random, oNo, 1,% obj.Length()
Random, iNo, 1,% obj[oNo].Length()
mx := obj[iNo].x
my := obj[iNo].y
MouseMove, %mx%, %my%
Sleep 200
Send ^{Click}
Sleep 50


obj.RemoveAt(iNo)  ; Remove used element

Random, oNo, 1,% obj.Length()
Random, iNo, 1,% obj[oNo].Length()
mx := obj[iNo].x
my := obj[iNo].y
MouseMove, %mx%, %my%
Sleep 200
Send ^{Click}
Sleep 50


if obj1.Length() + obj2.Length() = 55 ? : (obj1 := [ {x:1118,y:590}, {x:1119,y:640}, {x:1116,y:680}, {x:1119,y:735}, {x:1116,y:774}, {x:1170,y:591}, {x:1166,y:637}, {x:1166,y:687}, {x:1166,y:735}, {x:1167,y:781}, {x:1218,y:593}, {x:1213,y:639}, {x:1212,y:688}, {x:1215,y:741}, {x:1216,y:780}, {x:1260,y:590}, {x:1262,y:637}, {x:1263,y:687}, {x:1261,y:733}, {x:1264,y:784}, {x:1311,y:591}, {x:1309,y:639}, {x:1311,y:690}, {x:1311,y:734}, {x:1313,y:783}, {x:1358,y:591}, {x:1360,y:638}, {x:1359,y:688}, {x:1359,y:735}, {x:1359,y:782} ] & obj2 := [ {x:1408,y:590}, {x:1405,y:638}, {x:1404,y:686}, {x:1406,y:733}, {x:1405,y:782}, {x:1454,y:588}, {x:1451,y:638}, {x:1453,y:687}, {x:1454,y:735}, {x:1455,y:782}, {x:1503,y:587}, {x:1502,y:637}, {x:1503,y:687}, {x:1500,y:737} {x:1501,y:783}, {x:1550,y:588}, {x:1552,y:638}, {x:1549,y:686}, {x:1552,y:734}, {x:1551,y:783}, {x:1596,y:587}, {x:1597,y:634}, {x:1598,y:684}, {x:1598,y:734}, {x:1600,y:787}, {x:1649,y:589}, {x:1645,y:640}, {x:1647,y:687}, {x:1645,y:734}, {x:1649,y:782} ] )

MouseMove, 276, 224
Send {Space Down}
Sleep 200
Send {Space Up}
Sleep 50

Send {Esc}
MouseMove, 846, 280
Sleep 100
Send {Click}

return
My brain tells me tho that this impacts the "randomness" of the picking, because by rolling obj1 or obj2 I'm removing half of the pool... Not sure if that's true, just hard to imagine.
I have two more questions regarding this. First one is: Do I need to specify from which array the used object has to be removed? I kind of feel like by using obj.RemoveAt(iNo) it removes 2 objects with the same "key" (is that the right term?)
My second question is about the reset of the two arrays after picking 5 elements. Is using "&" the right way to do it?
mcl
Posts: 357
Joined: 04 May 2018, 16:35

Re: Trouble with Basics, pick 3 Elements out of 5

26 Nov 2020, 05:15

In the first code chunk use square braces, not curly ones. Curly braces are for objects, and objects require key-value pair.

Randomness is slightly affected, especially when one of two arrays have less elements in it.
About your questions:
1. Yes. Think about objects (and arrays) as folders on your disk. In the second code chunk obj.RemoveAt will remove either one of those two big arrays (obj1 or obj2) from obj, or nothing at all.
2. Well, using & will work, but it isn't convenient way to do so. And don't mix if-s with ternary operator, pick one:

Code: Select all

If (obj1.length() + obj2.length() == 55) {
	obj1 := [...]
	obj2 := [...]
}
One more thing. You don't have to add all elements to your array at once, you can add them in batches, so you won't get 'expression too long'.

Code: Select all

obj := []
obj.Push( {x:1118,y:590}, {x:1119,y:640}, {x:1116,y:680}, {x:1119,y:735}, {x:1116,y:774} )
obj.Push( {x:1170,y:591}, {x:1166,y:637}, {x:1166,y:687}, {x:1167,y:781}, {x:1166,y:783} )
obj.Push( {x:1218,y:593}, {x:1213,y:639}, {x:1212,y:688}, {x:1215,y:741}, {x:1216,y:780} )
...
Last edited by mcl on 26 Nov 2020, 05:57, edited 2 times in total.
github://oGDIp - GDI+ wrapper for AHK v1.1
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Trouble with Basics, pick 3 Elements out of 5

26 Nov 2020, 05:55

@iMMOLA - please review AHK's command reference regarding Array/Associative Array/Array Objects once more.

Code: Select all

array     := ["aaa",222,"ab12"] ; [array] items are accessed via its position. arr[3] => 'ab12' 
ascArray := {"key":"value",1:"aaa",2:222,3:variable,4:anotherObj} ; {associative array} items are accessed via its key. ascArray["key"] => 'value'
ascArray := {key:["value1","value2","value3"]} ; {assoc. array that keeps an [array] as value, items are accessed using key+postion ie. ascArray["key"][2] => 'value2'
I'd guess that in any case you can use object notation as well to access an arrays' item ie ascArray.key.3

HTH.
iMMOLA
Posts: 8
Joined: 17 Nov 2020, 05:31

Re: Trouble with Basics, pick 3 Elements out of 5

26 Nov 2020, 06:08

Oh my god, that did the trick. Thank you very much! :D

Thank you guys, really learned a lot here! :)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: just me and 154 guests