1

I'm trying to create an AHK script that will type a random insult whenever I type 'diss'. However, at the moment, all I am getting everytime I type 'diss' is "Array[rand]".

What am I doing wrong?

Array:= Object()

Loop, Read, C:\Users\dell\Desktop\insults.txt
{
    Array.Insert(A_LoopReadLine)
}


::diss::
Random, rand, 0, 3
Send, Array[rand]
Return

2 Answers2

2

Deceiving_Solicitite is technically correct... but I don't feel he explained his code very well and frankly the Hisham's code is just written better even if it's wrong (only by 2 small details). Now that that was said lets get to coding.

The fist issue that pops out to me is your Send command. Lets try changing Send, Array[rand] to Send % Array[rand] and now the code works most of the time...

But what does this mean?

Notice the % sign? What this is doing is Forcing an Expression on a command that doesn't normally evaluate expressions. Arrays/Objects are treated as such in AHK and Command's won't recognize them.

This still doesn't explain why the code only "works most of the time..."

Well as it turns out there's also a slight issue with the Random numbers he's generating. When Inserting Values into an Array they are Indexed incrementally starting at 1 and well his Random number is sometimes producing a zero. So we'll change that line now from to Random, rand, 1, 3 at this point the code is fixed and works %100 of the time.

But what if he wanted to have more insults than 3? Will he have to go through and count every line and amend his Random command?

That would be silly since we can have the the computer do it for us as it is much faster at counting lines of text than we are. So we'll simply have our Random produce results between 1 and the Max Index of our array like so: Random, rand, 1, % Array.MaxIndex() . Notice the %? We are again Forcing a Command to evaluate and expression. Neat huh?

Completed code:

Array:= Object()

Loop, Read, C:\Users\dell\Desktop\insults.txt
{
    Array.Push(A_LoopReadLine)
}

::diss::
Random, rand, 1, % Array.MaxIndex()
Send % Array[rand]
Return

I hope you learned from this.

Edit: Changed Array.Insert(), deprecated, to Array.Push().

errorseven
  • 2,672
  • 2
  • 14
  • 20
0

you forgot to set the name of the array in precent signs -> %A_index%

fileread,contents,C:\Users\dell\Desktop\insults.txt

Loop, parse, contents, `n, `r
{
array%a_index% := A_LoopField

}

return


::diss::


Random, rand, 1, 3
tosend = array%rand%
tosend = % %tosend%
Send, %tosend%
Return

fixed mutliple things..

1.try to read into variables

2.arrays can be used as variable names

3.varaible names can contain variables

report back if my code works for you and if you understand it.

/edit i fixed the code and tested it.

for your information im nesting the name if the variable inside a variable. ahk magic

  • Okay so I don't understand your code. However, I have never used AHK before today so I'll read into all those things in the near future. However, your code didn't work for me- when I type diss, it just prints nothing, instead of one of the three lines I have in the text file... – Hisham Mohammed Aug 21 '15 at 20:02
  • it works for me! - make sure you are running the latest version – Deceiving_Solicitite Aug 21 '15 at 21:27
  • 1
    It actually works, I messed up something with the txt file. Thanks. – Hisham Mohammed Aug 22 '15 at 00:22