Eyeon:Script/Reference/Libraries/table/foreach

From VFXPedia

Jump to: navigation, search

Table


Contents

table : table.foreach

Arguments

table.foreach( table, command )

  • table (required, table)

A table containing the values to be processed.

  • command (required, string)

command (required, string)

  • index (optional, number)

A function which will receive the index and value for each element in the table.

Returns

Returns any non-nil value returned by the command function.

Remarks

The foreach function takes two arguments, a table, and a command which is the name of a valid function. The foreach function loops through the entries of the table, passing the index and value for each entry as arguments to the command function. If the command function returns a value which is not nil than the loop is broken, and the foreach command with the return value of the command function.

The command argument gives the foreach function amazing flexibility, since it can be used to run an arbitrary function on all entries in a table.

Requirements

  • eyeonScript 5.0
  • Fusion 5.0

Examples

--- simple example x = {1001, 104, 1000, 105} table.foreach(x, print)

-- a simple search routine to locate a -- value in a table


-- * see notes below

function find_val(term)

 return function(index, value)
   if value == term then
     return index
   end
 end

end

x = {"Fu", "eyeon", "compositing", "Fusion", "DFXPlus"} ret = table.foreach(x, find_val("Fusion"))

print(x[ret])

-- * the function above actually creates and returns another function -- this technique is called a closure, and works -- because eyeonScript permits functions to be assigned -- directly to variables


Tips for foreach (edit)

EyeonTips:Script/Reference/Libraries/table/foreach