Collections are the most mysterious area of Vbscript, if you ask me. I think they are somehow hardcoded into the script host and can't be tempered with directly, or at least that's what the docs seem to imply. The closest thing to lists in Vbscript are dynamic arrays, but you can't use those for your purposes either, since you can't redimension an array by popping an index in the middle. What I'd do is use string operations - Left(), Right(), Instr(), etc. - and following are a couple of functions that might come usefull. Actually I'd move to Python or something that supports lists, but that's a different story :) Anyways, here it goes...
'Appends newItem to the end of strList, using the 'delim' delimiter
'doesn't perform any checks, returns the new list
'
function ListAppend(strList, newItem, delim)
ListAppend = strList & delim & newItem
end function
'Adds newItem to the end of the list, using the 'delim' delimiter, only if newItem
'isn't already in the list. Returns the new list if the item was added, original list
'if the item was already present
'
function ListAdd(strList, newItem, delim)
if instr(strList, newItem) then
ListAdd = strList
else
strList = strList & delim & newItem
ListAdd = strList
end if
end function
'Removes the removeItem from the list if it is present there,
'returns the new list if the item was removed, original list if the item wasn't
'found.
'
function ListRemove(strList, removeItem, delim)
dim startPos, endPos, resultList
startPos = instr(strList, removeItem)
if startPos then
endPos = instr(startPos, strList, delim)
if endPos then
resultList = Left(strList, startPos - 1) + Right(strList, len(strList) - endPos)
ListRemove = resultList
else
resultList = Left(strList, startPos - 1)
ListRemove = resultList
end if
else
ListRemove = strList
end if
end function
'Example of usage:
dim mobList
mobList = "orc_dragon_cat"
sub TestLists(name, output, wildcs)
'Add "dog" to the mobList
mobList = ListAdd(mobList, "dog", "_")
world.Note mobList
'Remove "cat" from the mobList
mobList = ListRemove(mobList, "cat", "_")
world.Note mobList
'Add "cat" back to the list
mobList = ListAdd(mobList, "cat", "_")
world.Note mobList
'Remove "urchin" from the list, it's not there so the list isn't changed
mobList = ListRemove(mobList, "urchin", "_")
world.Note mobList
'Appends another "cat" to the end of the list
mobList = ListAppend(mobList, "cat", "_")
world.Note mobList
end sub
These took about 15 minutes to write, and although I tested them (using the TestLists sub above) there might be some quirks left. |