Arrays
queste
22 Jan 2020, 13:56I'm looking to implement an form of array. I have 64 integers. If I had an array and wanted to check the 7th value I would query ArrayName(7). I believe I could use dictionaries here with key as the array number.
But would this work if the key, or array value position, is based upon an expression? For example ArrayName(7 + a variable), or Arrayname (a variable).
If not with dictionaries or lists can this be achieved in Quest?
Update: OKAY -it seems lists can partially do what I want, I can reference the 5th+variable item in a list.
Therefore how do I change the value of a list item?
So taking the fifth item in my list as an eample, do I need to create a second list that contains list items 0-4 from the original list, a third list with my new 5th item and a fourth list containing list items 6-64 from the original list. Then join them together and somehow replace the original list with the new list?

Io
22 Jan 2020, 14:32It should work, no problem. Though Quest has a different syntax than what you provided, the general idea does indeed work.
Edit: There should be no need to change a list item in a roundabout way. Just something like
StringListItem(ListName, Index)=10
should do it.
Edit Edit: No, this... apparently doesn't work?! What the hell, Quest?
queste
22 Jan 2020, 15:10Thanks anyway, didn't think it would be that easy. I'll just go with the more roundabout method.
mrangel
22 Jan 2020, 18:36Sadly, Quest's list types don't allow you to change an item except by removal, or by adding an item to the end.
However,
So taking the fifth item in my list as an eample, do I need to create a second list that contains list items 0-4 from the original list, a third list with my new 5th item and a fourth list containing list items 6-64 from the original list. Then join them together and somehow replace the original list with the new list?
This is more work than you need to do.
A naive solution would be to remove all the items after the one you want to change, put them into another list, and then add them back after making the change; but this isn't feasible because you can only remove by value. You can't remove an item from the list if there's an identical item earlier in the list, because list remove
removes the first match.
So the most efficient way to do this is actually:
<function name="SetListItem" parameters="list, itemnumber, value">
for (i, 0, ListCount (list) - 1) {
item = ListItem (list, 0)
list remove (list, item)
if (i = itemnumber) {
item = value
}
list add (list, item)
}
</function>
That loops over the list, removing every item from the beginning and putting it back on the end; except for the one you want to change, which is removed and then a different value added back to the end instead.
If you want to make it work on indexes past the end of the list, you'd do something like this:
<function name="SetListItem" parameters="list, itemnumber, value">
while (itemnumber >= ListCount (list)) {
list add (list, "")
}
if (itemnumber = ListCount (list)) {
list add (list, value)
}
else {
for (i, 0, ListCount (list) - 1) {
item = ListItem (list, 0)
list remove (list, item)
if (i = itemnumber) {
item = value
}
list add (list, item)
}
}
</function>
(padding the list out with empty strings until it reaches the desired length)
Using dictionaries is likely going to be simpler; just use ToString() on the index, and remember that if you want to add or remove items, it's up to you to renumber the later ones. Or create a linked list using objects, which I think should be feasible.
queste
23 Jan 2020, 07:46Thanks, mrangel, I'll see if I can make it work.