09-05-2018, 07:50 PM
I'm not sure what you really want, but try this to see if it fits:
Macro Weighted Random Choice
Macro Weighted Random Choice
;dictionary fruit.txt would have several arrays similar to this:
;{
;"apple", 9
;"pear", 1
;"banana", 10
;"orange", 20
;}
;Suppose the file dictionary fruit.txt is in the "Desktop" folder.
;You could change the directory to where the file is actually belongs to.
str s1.getfile("$desktop$\fruit.txt")
;Create an IStringMap variable to hold the result and weight values.
IStringMap m._create
lpstr fruit_mapping=sub.createDictMapping(s1)
;Assigned fruit list to IStringMap
m.AddList(fruit_mapping)
;Retrieve the result
out
ARRAY(str) allKeys, allValues
m.GetAll(allKeys, allValues)
int i, sum
for i 0 allValues.len
,sum +=val(allValues[i])
for i 0 allKeys.len
,_s = allKeys[i]
,out F"{_s} = {m.Get(_s)}/{sum}"
#sub createDictMapping
function~ $dict
str line
foreach line dict
,if(line = "{") continue
,if(line = "}") continue
,line.findreplace("''" "" 8)
,line.findreplace("," "" 8)
,_s.addline(line)
ret _s