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.
Output a random combination from specific strings Topic is solved
Re: Output a random combination from specific strings
Just out of curiosity - would you mind to explain why you've created multiple threads on the same topic within a few minutes?
Re: Output a random combination from specific strings
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 wrote:Just out of curiosity - would you mind to explain why you've created multiple threads on the same topic within a few minutes?
Re: Output a random combination from specific strings
Thx for the clarification as it confirms what I've expected to be the main reason.vane42 wrote: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 wrote:Just out of curiosity - would you mind to explain why you've created multiple threads on the same topic within a few minutes?
Re: Output a random combination from specific strings
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
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
Re: Output a random combination from specific strings
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)?
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)?
Re: Output a random combination from specific strings Topic is solved
Code: Select all
:*:aa1::
:*:aa2::
:*:aa3::
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
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
Re: Output a random combination from specific strings
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;::
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;::
Re: Output a random combination from specific strings
The backtick ` is the standard AHK escape character:
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.
See https://autohotkey.com/docs/commands/_EscapeChar.htmThe escape character is used to indicate that the character immediately following it should be interpreted differently than it normally would.
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.
Source: see link aboveNote: 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.
Re: Output a random combination from specific strings
Hi gregster,
Thank you for your elaboration, which makes me understand better.
And Odlanir, your code really helps me a lot, much appreciated.
Thank you for your elaboration, which makes me understand better.
And Odlanir, your code really helps me a lot, much appreciated.
Re: Output a random combination from specific strings
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.Odlanir wrote:What about this ?