Send random predefined variable labeled by number, if variable exists?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Stache
Posts: 6
Joined: 18 Aug 2022, 00:58

Send random predefined variable labeled by number, if variable exists?

Post by Stache » 18 Aug 2022, 02:14

I'm trying to make a simple script that sends a message telling me to perform 1 set of 2 actions out of 1000 sets, but only sets 1 and 2 exist.
I want it to keep rolling until it gets a number that does exist (1 or 2) in the event I want to add more sets in the future. How could I achieve that?

On top of that, how can I make the numbered variable be used without error?

Here's what I've come up with so far:


Code: Select all

^F7::

action1 := "Do the dishes and "
subaction1 := "Clean your room"

action2 := "Brush your teeth and "
subaction2 := "Go to bed"

random var, 1,1000
send %action%var%% %subaction%var%%
return

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Send random predefined variable labeled by number, if variable exists?

Post by BoBo » 18 Aug 2022, 02:43

Code: Select all

 ^F7::

act := ["Do the dishes and", "Brush your teeth and"]
sub := ["clean your room!", "go to bed", "do your homework", "fix the car", "better call Saul!"]

random, actPos, 1, act.Count()
random, subPos, 1, sub.Count()
send % act[actPos] . A_Space . sub[subPos]
return
Not tested.

Stache
Posts: 6
Joined: 18 Aug 2022, 00:58

Re: Send random predefined variable labeled by number, if variable exists?

Post by Stache » 18 Aug 2022, 03:22

Thanks for your help, Bobo.

This does randomize act and sub, but the strings in quotations are not part of the same set.
Examples of what your code can output: "Do the dishes and go to bed" and "Brush your teeth and clean your room!"

Also, I would like them to be organized and not on the same line. I plan to have lots of sets and wouldn't be very easy to manage if they were on just two lines.

Thanks again.

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Send random predefined variable labeled by number, if variable exists?

Post by BoBo » 18 Aug 2022, 03:47

Stache wrote:
18 Aug 2022, 03:22
This does randomize act and sub, but the strings in quotations are not part of the same set.
No problem as you can name the array accordingly act1/sub1…

Also, I would like them to be organized and not on the same line. I plan to have lots of sets and wouldn't be very easy to manage if they were on just two lines.
No problem as AHK allows to set up the code like below…

Code: Select all

#SingleInstance Force

act1 := ["Do the dishes and"
        ,"Clean the carpet and"]
act2 := ["Brush your teeth and"
        ,"Take a shower and"]
        
sub1 := ["clean your room!"
        ,"empty that bin."]
Sub2 := ["go to bed!"]

F1::
F2::
   setNo := SubStr(A_ThisHotkey, 2)
   random, actPos, 1, act%setNo%.Count()
   random, subPos, 1, sub%setNo%.Count()
   MsgBox % act%setNo%[actPos] . A_Space . sub%setNo%[subPos]	;replace 'MsgBox' with 'Send' after testing.
   return

Edit: fixed & tested.

Stache
Posts: 6
Joined: 18 Aug 2022, 00:58

Re: Send random predefined variable labeled by number, if variable exists?

Post by Stache » 18 Aug 2022, 04:14

Sorry bobo, it appears that the code you posted results in an error.

Image
Attachments
image.png
image.png (13.58 KiB) Viewed 959 times

just me
Posts: 9456
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Send random predefined variable labeled by number, if variable exists?

Post by just me » 18 Aug 2022, 04:34

Hi,

if you want to keep your original concept just use expression syntax to access the variables:

Code: Select all

#NoEnv
Action1 := "Do the dishes and "
Subaction1 := "Clean your room"

Action2 := "Brush your teeth and "
Subaction2 := "Go to bed"

NumOfActions := 2
Return

^F7::
Random, R, 1, %NumOfActions%
Send % Action%R% . Subaction%R% ; % -> force an expression
Return
But I also recommend to use simple arrays.

Stache
Posts: 6
Joined: 18 Aug 2022, 00:58

Re: Send random predefined variable labeled by number, if variable exists?

Post by Stache » 18 Aug 2022, 04:46

just me wrote:
18 Aug 2022, 04:34
Hi,

if you want to keep your original concept just use expression syntax to access the variables:

Code: Select all

#NoEnv
Action1 := "Do the dishes and "
Subaction1 := "Clean your room"

Action2 := "Brush your teeth and "
Subaction2 := "Go to bed"

NumOfActions := 2
Return

^F7::
Random, R, 1, %NumOfActions%
Send % Action%R% . Subaction%R% ; % -> force an expression
Return
But I also recommend to use simple arrays.
This seems to have worked. Thank you for your help, greatly.

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Send random predefined variable labeled by number, if variable exists?

Post by BoBo » 18 Aug 2022, 05:52

@Stache - I've tested & fixed the code above. JFTR.

Post Reply

Return to “Ask for Help (v1)”