Sign in to follow this  
Followers 0
Damein

Little help with _ArraySearch

4 posts in this topic

So I'm doing a random function for a file and I want to make sure the same item is not selected twice. Here is the code I made, but it doesn't work how I want it, I think I'm missing something.

#include <GUIConstantsEx.au3>
#include <IE.au3>
#include <Array.au3>
#include <WindowsConstants.au3>

Dim $RandomNumber[1]

Opt("GUIOnEventMode", 1)
Opt("WinTitleMatchMode", 1)

GuiCreate("Test", 400,500)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Closed")
$PlayListView = GuiCtrlCreateList("", 10,10,300,400, BitOR($WS_BORDER, $WS_VSCROLL))
$PlayListSongs = IniReadSection("Test.ini", "Country")
For $i = 0 To $PlayListSongs[0][0]
GUICtrlSetData($PlayListView, $PlayListSongs[$i][0])
Next
$RandomButton = GuiCtrlCreateButton("Random", 160,450)
GUICtrlSetOnEvent($RandomButton, "_RandomList")
GuiSetState()

Func _RandomList()
For $i = 1 To $PlayListSongs[0][0] Step +1
$SelectRandomNumber = Random(1,$PlayListSongs[0][0],1)
$iIndex = _ArraySearch($RandomNumber, $SelectRandomNumber)
If @error Then
_ArrayAdd($RandomNumber, $PlayListSongs[$SelectRandomNumber][0])
Else
$i -= 1
EndIf
Next
_ArrayDisplay($RandomNumber, "Test")
EndFunc

Func _Closed()
Exit
EndFunc


While 1
Sleep(10)
WEnd

The problem is the same song is loaded more than once, when it shouldn't be, because I only add a new _ArrayAdd whenever the search comes back as not found, so it shouldn't happen (I thought). So I must be missing something.

Here is the ini file.

[Country]
=
Boondocks=http://www.youtube.com/watch?v=4-XfthjK-bk
Amarillo Sky=http://www.youtube.com/watch?v=OaYmhWxWHY8
Water=http://www.youtube.com/watch?v=NS8yDc2RpD8&feature=topvideos_music
Austin=http://www.youtube.com/watch?v=bb1DTsxBOfE
Are You Gonna Kiss Me Or Not=http://www.youtube.com/watch?v=2qkHZMS5lW8
The Gambler=http://www.youtube.com/watch?v=rBCxcEkkXFo
Coward Of The County=http://www.youtube.com/watch?v=yK543f0_UKc
Private Malone=http://www.youtube.com/watch?v=0gxLEVg5aHM
Wrapped Around=http://www.youtube.com/watch?v=w6PkOuE9PQQ
Stuck Like Glue=http://www.youtube.com/watch?v=5Q9Gou6d9Uo&feature=grec_index
What was I thinkin=http://www.youtube.com/watch?v=kAzp8FXA-FQ
She's Everything=http://www.youtube.com/watch?v=YsQmkkmDYpk&feature=grec_index
Where I Come From=http://www.youtube.com/watch?v=ku126jOpYPU
Grundy County Auction=http://www.youtube.com/watch?v=I4--VnxoSCo
Old Alabama=http://www.youtube.com/watch?v=4dlzCv_-zN4
Country Girl=http://www.youtube.com/watch?v=pEUCuX5cFRA
Where The Blacktop Ends=http://www.youtube.com/watch?v=xhsO7LnwyBI
Hot Mama=http://www.youtube.com/watch?v=Rd5TJemW_iE
Ladies Love Country Boys=http://www.youtube.com/watch?v=mSNqZK4rvZw
Where Were You=http://www.youtube.com/watch?v=FyPtqvaKAfU
As Good As I Once Was=http://www.youtube.com/watch?v=ldQrapQ4d0Y&ob=av2e
Before He Cheats=http://www.youtube.com/watch?v=aBuLhwXPJls
Good Morning Beautiful=http://www.youtube.com/watch?v=wsVX1F0j1gI&feature=grec_index
Whiskey Lullaby=http://www.youtube.com/watch?v=7gV3g9LCvPc&feature=grec_index
Lover, Lover=http://www.youtube.com/watch?v=MKaJ9UTZUhQ
Where The Green Grass Grows=http://www.youtube.com/watch?v=-aENJoceTXA&feature=grec_index
Bless the Broken Road=http://www.youtube.com/watch?v=lZp6pmgbZyU&feature=grec_index
Must Be Doing Something Right=http://www.youtube.com/watch?v=mkUifSvubRg
Good Directions=http://www.youtube.com/watch?v=BWKqjy87hiY

Also, every once in awhile there is a blank array as well :D.

Thanks in advance!


MCR.jpg?t=1286371579

Most recent sig. I made

Quick Launcher W/ Profiles Topic Movie Database Topic & Website | LiveStreamer Pro Website | YouTube Stand-Alone Playlist Manager: Topic | Weather Desktop Widget: Topic | Flash Memory Game: Topic | Volume Control With Mouse / iTunes Hotkeys: Topic | Weather program: Topic | Paws & Tales radio drama podcast mini-player: Topic | Quick Math Calculations: Topic

Share this post


Link to post
Share on other sites



Oh, I see why. I forgot to add the number to an array, so its not actually saved. I'll post my edited version when I get done with it.


MCR.jpg?t=1286371579

Most recent sig. I made

Quick Launcher W/ Profiles Topic Movie Database Topic & Website | LiveStreamer Pro Website | YouTube Stand-Alone Playlist Manager: Topic | Weather Desktop Widget: Topic | Flash Memory Game: Topic | Volume Control With Mouse / iTunes Hotkeys: Topic | Weather program: Topic | Paws & Tales radio drama podcast mini-player: Topic | Quick Math Calculations: Topic

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Revised posting, fixed error.

#include <GUIConstantsEx.au3>
#include <IE.au3>
#include <Array.au3>
#include <WindowsConstants.au3>
Dim $RandomNumbers[1], $NewPlaylist[1]
Opt("GUIOnEventMode", 1)
Opt("WinTitleMatchMode", 1)
GuiCreate("Test", 400,500)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Closed")
$PlayListView = GuiCtrlCreateList("", 10,10,300,400, BitOR($WS_BORDER, $WS_VSCROLL))
$PlayListSongs = IniReadSection("Test.ini", "Country")
  For $i = 1 To $PlayListSongs[0][0]
   GUICtrlSetData($PlayListView, $PlayListSongs[$i][0])
  Next
$RandomButton = GuiCtrlCreateButton("Random", 160,450)
GUICtrlSetOnEvent($RandomButton, "_RandomList")
GuiSetState()
Func _RandomList()
For $i = 1 To $PlayListSongs[0][0] Step +1
$SelectRandomNumber = Random(1,$PlayListSongs[0][0],1)
$iIndex = _ArraySearch($RandomNumbers, $SelectRandomNumber)
If @error Then
;~   MsgBox(0, "Test", $SelectRandomNumber & " : " & $PlayListSongs[$SelectRandomNumber][0])
  _ArrayAdd($NewPlaylist, $PlayListSongs[$SelectRandomNumber][0])
  _ArrayAdd($RandomNumbers, $SelectRandomNumber)
Else
  $i -= 1
EndIf
Next
_ArrayDisplay($NewPlaylist, "Test")
EndFunc
Func _Closed()
Exit
EndFunc

While 1
Sleep(10)
WEnd
Edited by Damein

MCR.jpg?t=1286371579

Most recent sig. I made

Quick Launcher W/ Profiles Topic Movie Database Topic & Website | LiveStreamer Pro Website | YouTube Stand-Alone Playlist Manager: Topic | Weather Desktop Widget: Topic | Flash Memory Game: Topic | Volume Control With Mouse / iTunes Hotkeys: Topic | Weather program: Topic | Paws & Tales radio drama podcast mini-player: Topic | Quick Math Calculations: Topic

Share this post


Link to post
Share on other sites

You could also do it without using any of the slow _Array functions. Actually without any functions at all, except Rnd().

#include <GUIConstantsEx.au3>
#include <Array.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1)

Local $aInput = IniReadSection("test.ini", "Country") ; input array
Global $cnt = $aInput[0][0]

GuiCreate("Test", 400, 500, 150)
$PlayListView = GuiCtrlCreateList("", 10,10,300,400, BitOR($WS_BORDER, $WS_VSCROLL))
For $i = 1 To $cnt
   GUICtrlSetData($PlayListView, $aInput[$i][0])
Next
$RandomButton = GuiCtrlCreateButton("Random", 160,450)
GuiSetState()

GUICtrlSetOnEvent($RandomButton, "_Scramble_Songs")
GUISetOnEvent($GUI_EVENT_CLOSE, "_Close")

While 1
  Sleep(10)
WEnd
Exit

;-------------------------------------------------------------------------------
Func _Scramble_Songs()
  $aOutput = Randomize_Array2D($aInput)
  GUICtrlSetData($PlayListView, "")
  For $i = 1 To $cnt
    GUICtrlSetData($PlayListView, $aOutput[$i][0])
  Next
EndFunc

Func Randomize_Array2D($a_In) ; input array
 Local $a_Ctrl[$cnt + 1] ; control array
 For $i = 1 To $cnt
   $a_Ctrl[$i] = $i
 Next
 Local $a_Out[$cnt + 1][2] ; output array
 For $i = 1 To $cnt - 1
   $rnd = Random($i, $cnt, 1)
   $a_Out[$i][0] = $a_In[$a_Ctrl[$rnd]][0]
   $a_Out[$i][1] = $a_In[$a_Ctrl[$rnd]][1]
   $a_Ctrl[$rnd] = $a_Ctrl[$i]
 Next
 $a_Out[$cnt][0] = $a_In[$a_Ctrl[$cnt]][0]
 $a_Out[$cnt][1] = $a_In[$a_Ctrl[$cnt]][1]
 Return $a_Out
EndFunc

Func _Close()
 Exit
EndFunc

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0