how to shuffle elements in an array Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
arathra
Posts: 23
Joined: 29 Aug 2016, 06:22

how to shuffle elements in an array

25 Nov 2016, 12:47

I have a csv file which is read into an array. I'd like to be able to shuffle the elements in that array so that later when I loop through it the elements will display in a different order from the original csv file.

The CSV file is separated by a semi-colon something like this:

Code: Select all

word;language
hello;english
ciao;italian
giasou;greek
Here's my current code which is pretty straightforward:

Code: Select all

vFile := A_WorkingDir . "\words.csv"
Loop Read, %vFile%
{
	SubArray := StrSplit(A_LoopReadLine, ";")
	MyArray.Insert(SubArray)
}

; at this point I need to shuffle MyArray
I've tried using snippets of code I found in the archived forums here but after several hours messing around I just can't find a way of getting it to work. It's defeated me.

Has anyone a snippet of code showing how I can shuffle MyArray?
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: how to shuffle elements in an array  Topic is solved

25 Nov 2016, 13:17

1) Use Sort to randomize the string before you split it. You will need to use FileRead instead of Loop, Read.

Code: Select all

MyArray := []
Test := 
    (Join`r`n LTrim
       "word;language
        hello;english
        ciao;italian
        giasou;greek"
    )
Sort, Test, Random
Loop, Parse, Test, `n, `r
    MyArray.Push(StrSplit(A_LoopField, ";"))

for i, v in MyArray
    s .= i "=" v[1] "," v[2] "`n"
MsgBox, % s
Or 2) Pull an item out of the array at random and insert it into a new array.

Code: Select all

MyArray := [], NewArray := []
Test := 
    (Join`r`n LTrim
       "word;language
        hello;english
        ciao;italian
        giasou;greek"
    )
Loop, Parse, Test, `n, `r
    MyArray.Push(StrSplit(A_LoopField, ";"))
while MyArray.Haskey(1) {
    Random, n, 1, MyArray.MaxIndex()
    NewArray.Push(MyArray.RemoveAt(n))
}

for i, v in NewArray
    s .= i "=" v[1] "," v[2] "`n"
MsgBox, % s
Edit: Or 3) Use InsertAt to insert an item at a random index (between 1 and MyArray.MaxIndex() + 1).

Code: Select all

MyArray := []
Test := 
    (Join`r`n LTrim
       "word;language
        hello;english
        ciao;italian
        giasou;greek"
    )
Loop, Parse, Test, `n, `r
{
    if (A_Index = 1)  ; Insert the first one at MyArray[1]
        MyArray[1] := StrSplit(A_LoopField, ";")
    else {  ; Insert at a random index
        Random, n, 1, MyArray.MaxIndex() + 1
        MyArray.InsertAt(n, StrSplit(A_LoopField, ";"))
    }
}

for i, v in MyArray
    s .= i "=" v[1] "," v[2] "`n"
MsgBox, % s
Edit2: I suggest #1.
arathra
Posts: 23
Joined: 29 Aug 2016, 06:22

Re: how to shuffle elements in an array

07 Dec 2016, 12:18

Thank you very much, Kon.

I didn't use the first solution as the list of rows in the CSV is very long indeed. And the third solution I couldn't get to work. But the second one came out fine.

For reference this is my final code:

Code: Select all

vURLFile := A_WorkingDir . "\webpages.csv"
Loop Read, %vURLFile%
{
	SubArray := StrSplit(A_LoopReadLine, ";")
	URLArray.Insert(SubArray)
}
TempArray := Object()
while URLArray.Haskey(1) {
    Random, n, 1, URLArray.MaxIndex()
    TempArray.Push(URLArray.RemoveAt(n))
}
URLArray := TempArray
TempArray := ""
Again, thanks for your help Kon!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 362 guests