** WARNING: this post is probably long, but maybe I need this much detail to explain it all **
Okay, I'm going to try to explain this as best as I can, because at this point my project has become confused and wrapped around itself.
First, I have an object I've created, it stores 3 simple properties, a String for a name, an integer for the Cost of the object, and a boolean/integer for whether it is a certain type of object or not.
So, for instance:
"Chair", 10, 1 - This is an object with the name Chair, a Cost of 10, and a 1 to indicate it is a particular type of object (a 0 indicates a different type).
I have a file I read these objects in from, line by line, in the format of Name, then a series of special characters to form a delimeter, then the Cost number, special characters for delimeter, then the object type.
So, I'd have...
Chair=+=10=+=1
Hat=+=5=+=0
Car=+=10=+=1
Etc.
Several of these objects that I'm pulling in are sharing Costs, for instance, I have multiple items with Cost 10.
I have had no issue using StrSplit and other functions to read the items all into a linear array, so I can pick them out one by one and work with them.
One function I have, however, is trying to find a RANDOM item by highest Cost. In the example above, the highest Cost items are Chair and Car, so it would randomly return one of those two.
I got the idea to store them into an associative array - that is, feed through the main array, item by item check the Cost, then store them in an associative array, where the key is the Cost.
Then it occurred to me I need each of those entries in the associative array to itself be an array, to store multiple items by the same "key".
So I now have an Associative Array (keys are the shared Cost of the objects), of simple arrays (each entry being an object with that same Cost).
I can fetch the highest Cost items, by going to the Associative Array's max index.
I can go to that referenced array, and pick a random item from it.
The problem comes when I want to remove the selected object from the original linear array, so that when this runs again, I have the remaining highest cost items, and so on, until I've exhausted the original array.
How can I programmatically remember WHERE that object came from in the original linear array?
I cannot just replace the external array with my new associative array, as other functions require the objects to be in a linear array, and not in a potentially sparse associative array.
Here's something I have so far
Code: Select all
RandomByHighest(ByRef arg_Array)
{
local_Array := Array()
For index, myObj in arg_Array ; Build the AssocArray here
{
local_Cost := myObj.Cost
local_Array[local_Cost].Push(myObj)
}
local_HighestCost := local_Array.MaxIndex()
local_Max := local_Array[local_HighestCost].MaxIndex()
Random, local_Rand, 1, local_Max
local_MyNewObj := local_Array[local_HighestCost][local_Rand]
; So now, I've populated local_MyNewObj with a random object, in the highest cost key, of the whole associative array.
; However, I want to do:
; arg_Array.RemoveAt(the_index_of_my_local_MyNewObj)
; How do I remember that index to remove the chosen object from the original linear array
}