Output a random combination from specific strings Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
vane42
Posts: 11
Joined: 24 Jan 2018, 02:56

Output a random combination from specific strings

24 Jan 2018, 02:59

Hi,

I would like to know how to output a random combination from specific strings and give a delimiter.

For example, there is a string list as shown below.
String1= "#abc"
String2= "de ox"
String3= "01-33@"
String4= "2014 8261.553,,583"

* The delimiter is & (and character).
* There is no repeated strings.

When I press the hotkeys [str1], then output 1 string randomly. >> "01-33@" 【any 1 string without delimiter】
When I press the hotkeys [str2], then output 2 strings randomly. >> "de ox&#abc" 【any 2 strings with delimiter】
When I press the hotkeys [str3], then output 3 strings randomly. >> "#abc&2014 8261.553,,583&de ox" 【any 3 string with delimiter】
And so on....

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

Re: Output a random combination from specific strings

24 Jan 2018, 03:15

Just out of curiosity - would you mind to explain why you've created multiple threads on the same topic within a few minutes? :eh:
vane42
Posts: 11
Joined: 24 Jan 2018, 02:56

Re: Output a random combination from specific strings

24 Jan 2018, 03:31

BoBo wrote:Just out of curiosity - would you mind to explain why you've created multiple threads on the same topic within a few minutes? :eh:
I had thought my post was not successful and then I just noticed that there is a review period for a new post. Sorry~
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Output a random combination from specific strings

24 Jan 2018, 04:40

vane42 wrote:
BoBo wrote:Just out of curiosity - would you mind to explain why you've created multiple threads on the same topic within a few minutes? :eh:
I had thought my post was not successful and then I just noticed that there is a review period for a new post. Sorry~
Thx for the clarification as it confirms what I've expected to be the main reason. :thumbup:
Odlanir
Posts: 659
Joined: 20 Oct 2016, 08:20

Re: Output a random combination from specific strings

24 Jan 2018, 08:47

What about this ?

Code: Select all

strings =
(
#abc
de ox
01-33@
2014 8261.553,,583
aaa bbb ccc
123 456 789--aaa bbb 
)
arr := StrSplit(strings, "`n")
return 

::str1::
::str2::
::str3::   
   sendraw, % CreateString(substr(A_ThisHotkey, 6,1) )
return

CreateString(val) {
   global arr
   arrtmp := arr.clone(), strOU := ""   
   loop, % val {      
      Random, rnd, 1, % max := arrtmp.length()
      strOU .= arrtmp[rnd] "&"
      arrtmp.Remove(rnd)
   }   
   return, substr(strOU,1,-1)
}
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
vane42
Posts: 11
Joined: 24 Jan 2018, 02:56

Re: Output a random combination from specific strings

24 Jan 2018, 11:17

Hi Odlanir,

Thank you for your reply.

I still have some questions.

1. If I change the hotkey from strX to aaX;, X means the number of output string, and add a semicolon after X, which line should I modify to get the number? How to output a combination directly after hotkey aaX; without pressing other keys?

2. What does 6 stand for in substr(A_ThisHotkey, 6,1)?
Odlanir
Posts: 659
Joined: 20 Oct 2016, 08:20

Re: Output a random combination from specific strings  Topic is solved

24 Jan 2018, 13:03

Code: Select all

:*:aa1::
:*:aa2::
:*:aa3::
this will execute the code without pressing any other key, just after you enter one of the hotsrings.
The substr function extracts the number ( 1,2,3) from the hotstring name. So only one function is called to execute the code.
I don't understand the semicolon request, but if you want to terminate the hotsrings with the semicolon this is the way to go:

Code: Select all

:*:aa1`;::
:*:aa2`;::
:*:aa3`;::   
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
vane42
Posts: 11
Joined: 24 Jan 2018, 02:56

Re: Output a random combination from specific strings

25 Jan 2018, 02:45

Hi Odlanir,

Thank you for your explanation.
I tried the both below, they all works. When to use backquote or what is a backquote's use?
:*:aa1`;::
:*:aa1;::
gregster
Posts: 9020
Joined: 30 Sep 2013, 06:48

Re: Output a random combination from specific strings

25 Jan 2018, 03:01

The backtick ` is the standard AHK escape character:
The escape character is used to indicate that the character immediately following it should be interpreted differently than it normally would.
See https://autohotkey.com/docs/commands/_EscapeChar.htm

I think Odlanir deemed it necessary in this case, because a semicolon normally indicates the beginning of a same-line comment in AHK. A backtick would make it a literal semicolon instead.

But that is not necessary in this case, because there is no space or tab in front of the; (which is necessary for creating a same-line comment), but on the other hand the escape character doesn't hurt here either. That's why both variants work.
Note: It is not necessary to escape a semicolon which has any character other than space or tab to its immediate left, since it would not be interpreted as a comment anyway.
Source: see link above
vane42
Posts: 11
Joined: 24 Jan 2018, 02:56

Re: Output a random combination from specific strings

25 Jan 2018, 03:16

Hi gregster,

Thank you for your elaboration, which makes me understand better. :thumbup:

And Odlanir, your code really helps me a lot, much appreciated. :bravo:
vane42
Posts: 11
Joined: 24 Jan 2018, 02:56

Re: Output a random combination from specific strings

25 Jan 2018, 20:54

Odlanir wrote:What about this ?
A new question is there are other hotstrings in my AutoHotkey.ahk. It only works when your code is in the first line. If I want to change it to different line, what should I do? Thanks.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: filipemb, mikeyww and 262 guests