Pick random line from .txt file Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
wyw
Posts: 93
Joined: 12 Dec 2015, 19:11

Pick random line from .txt file

27 Mar 2018, 10:26

I have a script that picks up a random word in the list. I changed the script multiple times thanks to Google (research), but it wasn't the way I wanted it to.

All I want the script to do is send a random line from the text file, by pressing F3 once. The text file contains of text like:

G 6:
start new func(125
G 7:
start new func(130

The thing is, the script should pickup "two lines", lines like:
G 6:
start new func(125

So that G 6 and start new func are basically together, and not only G 6:.

Here's my current script:



Code: Select all

list =
(
)
; FileRead, list, % E:\Decom\monsters.txt ; <<<< in order to load the list from a file instead
o := (obj:=StrSplit(list, "`n")).clone()
l := length := obj.length()

F3::
(l or (o:=obj.clone(), l:=length))
random, r, 1, % l ; generates a random number between 1 and the current length of the array of words
SendInput % obj.removeAt(r) ; remove the word from the given random position in the array returning it
--l
return
F4:: ; press control+r to reload
    Reload
  return

As always, it would be cool if someone helps me out.
donovv
Posts: 108
Joined: 15 Apr 2017, 21:06

Re: Pick random line from .txt file

27 Mar 2018, 12:41

im at work but this might give you a few idea's

Code: Select all

string1 := "G6: | start new func(125"
string2 := "G7: | start new func(130"

outputstring := ""

f1::
	random, r , 1,2
	pick := string%r%
	outputstring := strreplace(pick, "|","`n") 
	msgbox %outputstring%
return
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: Pick random line from .txt file

27 Mar 2018, 15:57

Example:

Code: Select all

#NoEnv
SetWorkingDir, %A_ScriptDir%

; Testing:
list =
(join`n`r
G 6: 
start new func(125
G 7: 
start new func(130
G 8: 
start new func(135
G 9: 
start new func(140
)
; FileRead, list, % E:\Decom\monsters.txt ; <<<< in order to load the list from a file instead

obj := []                         ; Create original obj to clone from
Loop, Parse, list, `n, `r
    (Mod(A_Index,2) = 0) ? (obj.Push(Line1 . "`n" . A_LoopField)) : (Line1 := A_LoopField)
return

F3::
    if (!o.length())
        o := obj.Clone()          ; Clone orignal obj when o is empty or not created yet.
    random, r, 1, % o.length()    ; generates a random number between 1 and the current length of the array of words
    SendInput % o.removeAt(r)     ; remove the word from the given random position in the array returning it
return
wyw
Posts: 93
Joined: 12 Dec 2015, 19:11

Re: Pick random line from .txt file

27 Mar 2018, 16:36

Thank you both for your answers. @donovv, just so you know, Xtra's script is easier to use, because if I go into your direction of your script, I would have to add over 500 lines (string1 := "G6: | start new func(125"
string2 := "G7: | start new func(130"( etc. That would take veeeery long. But thanks anyways.


@Xtra, thanks for your script. It's kind of what I need. The thing is, I didn't mention that my txt file contains over 500 lines (hence txt file). If I'd add all these lines into one script, I would get "Continuation section too long".
Now the thing is, I don't get why the script isn't picking up the lines in the monsters.txt file. I removed the "list =" code (if that's even right) and the script doesn't do anything. Can you help me here a bit?
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: Pick random line from .txt file  Topic is solved

27 Mar 2018, 17:52

Use it like this:

Code: Select all

#NoEnv
SetWorkingDir, %A_ScriptDir%
FileRead, list, E:\Decom\monsters.txt    ; Load the list from a text file

obj := []                         ; Create original obj to clone from
Loop, Parse, list, `n, `r
    (Mod(A_Index,2) = 0) ? (obj.Push(Line1 . "`n" . A_LoopField)) : (Line1 := A_LoopField)
return

F3::
    if (!o.length())
        o := obj.Clone()          ; Clone orignal obj when o is empty or not created yet.
    random, r, 1, % o.length()    ; generates a random number between 1 and the current length of the array of words
    SendInput % o.removeAt(r)     ; remove the word from the given random position in the array returning it
return
wyw
Posts: 93
Joined: 12 Dec 2015, 19:11

Re: Pick random line from .txt file

28 Mar 2018, 06:14

Xtra wrote:Use it like this:

Code: Select all

#NoEnv
SetWorkingDir, %A_ScriptDir%
FileRead, list, E:\Decom\monsters.txt    ; Load the list from a text file

obj := []                         ; Create original obj to clone from
Loop, Parse, list, `n, `r
    (Mod(A_Index,2) = 0) ? (obj.Push(Line1 . "`n" . A_LoopField)) : (Line1 := A_LoopField)
return

F3::
    if (!o.length())
        o := obj.Clone()          ; Clone orignal obj when o is empty or not created yet.
    random, r, 1, % o.length()    ; generates a random number between 1 and the current length of the array of words
    SendInput % o.removeAt(r)     ; remove the word from the given random position in the array returning it
return
Thanks Xtra! That did it for me!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: jameswrightesq, wpulford and 396 guests