Name
NextItem -- traverse fields of a table (V2.0)
Synopsis
next, item = NextItem(table[, start])
Function
NextItem() returns the item that follows after the item start in the specified table. If start is Nil, the first table item is returned. If there is no item after start, Nil is returned.

This function is mostly used to traverse all fields of the table in argument 1. To do this, you pass the table in argument 1 and leave out the second argument. NextItem() then returns an index to the next value in the table and the value at that table index. To traverse all fields, you have to pass the next value to NextItem() as the second argument and loop over it until the next value is Nil.

When there are no more items in the table, Nil is returned and you can terminate your loop. Be careful when checking variables against Nil because 0=Nil is actually True in Hollywood. Thus, GetType() is the only reliable way to find out if a variable is really Nil. Simply checking it against Nil would also result in True if the variable was 0.

Do not expect this function to return the table fields in the order they were assigned. Hollywood often stores them in a different order.

Inputs
table
table to traverse
start
optional: where to start the traversal (defaults to Nil which means start at the beginning)
Results
next
index of the next table item after start or Nil if there are no more items
item
table value of the item next to start
Example
t = {1, 2, 3, 4, 5, "Hello World", {100, 200, 300}, [-1.5] = -1.5,
     b = 66, Function(s) DebugPrint(s) EndFunction}
a, b = NextItem(t)
While GetType(a) <> #NIL
  DebugPrint(b)
  a, b = NextItem(t, a)
Wend
The above code traverses a heterogenous table. The output will be the following:
 
2
3
4
5
Hello World
Table: 74cbd42c
Function: 74cbd3c8
1
-1.5
66

You see that the fields are returned in a different order than they were assigned.


Show TOC