Iterating through Squirrel Table in imp.wakeup()

I need to iterate through a table 1 index at a time each time an imp.wakeup loop executes. Searching through the Squirrel documentation it looks like the _nexti() metamethod is what I need but I can’t seem to figure the syntax out. Here is what I was thinking:

`
tbl <- {
key1 = “val1”,
key1 = “val2”,

keyN = “valN”,
}

key <- null;
function loop(){
key = tbl._nexti(key)

//Do something with the key on this iteration

 imp.wakeup(0.01, loop)

}
loop()
`

Another option is to keep an array of the sorted keys and loop through it - unfortunately I have a LOT of keys and am bumping up against RAM constraints…

Any ideas on how to get this to work?

This could be done with a generator.
`tbl <- {
key1 = “val1”,
key1 = “val2”,

keyN = “valN”,
}

function mainloop(myTable)
{
foreach(key,value in myTable) {
// do something with key and value
yield pause(0.01);
}
}

function pause(duration)
{
return imp.wakeup(duration, function(){resume loop;});
}

loop <- mainloop(tbl);
resume loop;`

You beat me to it, @coverdriven. @deldrid1, more info on generator functions here

I had considered a generator but I haven’t used them yet myself - looks like something to try.

Are there any simpler alternatives or is a table iterator something that isn’t supported by Squirrel?

[DELETED]

You don’t get to call metamethods directly (for some reason), so no, there’s no such thing as a “free” iterator. Coverdriven’s answer is the best way of doing it.

Peter

@peter Any interest in adding a callmetamethod(“strmetamethod”, args) function to imp Squirrel for a use case like this? :slight_smile: