Jump to content

Choosing random values of an array and deleting them


 Share

Recommended Posts

Hey, ive had a look up on arrays, and they are not my friend to say the least..I was just wondering if it was possible (it probably is) to have an array, lets say of 50 elements, that are read out of a .ini file eg...

[Words]

1=cat

2=dog

3=cow

4=horse

.

.

.

.

50=sheep

And for one term to be chosen at random..and for that term to be removed, so the .ini may look something like....

[Words]

1=cat

2=dog

4=horse

.

.

.

.

50=sheep

Just at this point im wondering if i actually need an array to do this at all, or if there would be another method, but i cant think of one.

Is it possible for the array to know that there are now 49 terms AND that it can't choose term number 3 as it does not exist? I sort of need it to keep choosing a random term until term list is empty...is this possible??

Thank you in advance

Instant Lockerz Invite - www.instantlockerzinvite.co.uk
Link to comment
Share on other sites

Hey, ive had a look up on arrays, and they are not my friend to say the least..I was just wondering if it was possible (it probably is) to have an array, lets say of 50 elements, that are read out of a .ini file eg...

[Words]

1=cat

2=dog

3=cow

4=horse

.

.

.

.

50=sheep

And for one term to be chosen at random..and for that term to be removed, so the .ini may look something like....

[Words]

1=cat

2=dog

4=horse

.

.

.

.

50=sheep

Just at this point im wondering if i actually need an array to do this at all, or if there would be another method, but i cant think of one.

Is it possible for the array to know that there are now 49 terms AND that it can't choose term number 3 as it does not exist? I sort of need it to keep choosing a random term until term list is empty...is this possible??

Thank you in advance

Just deleting a random entry, if you know the keys are 1 thru 50, is just:
IniDelete ( "C:\YourFile.ini", "Words", String(Random(1, 50, 1)))

If the numbers are not consecutive, or the key names are completely unknown, then you certainly need to use arrays. Just read the whole section with IniReadSection(), delete one entry from the array, then IniWriteSection(). Done in three lines of code.

:P

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

dim $var,$randomnumber,$deletedarray,$search

$var = IniReadSection("filename", "Words")   ; this would $var[0][0] would give the amount of keys.

for $i=1 to $var[0][0]
do
$randomnumber=Random(1,$var[0][0],1)
$search=_ArraySearch($deletedarray, $randomnumber)
Until @error<>6
IniDelete ( "filename", "Words","$randomnumber")
$deletedarray[$i]=$randomnumber
Next

this code should work only problem is after most of the entries are deleted it might take some time before 1 left element gets picked.

you might try to find a way so you can detect when you only need to generate from lets say number x to y. instead of the whole ini.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...