Tabel Table - arrays and iterators

# Obtain tabel (key-value pairs) the maximum number of table index, tabel not the
array in Lua:
Array = { "Lua", "C #"}
1, 1 starting from the default index
2, the index may be negative (key-value pairs in the form of table)
Array = {};
for I = do-2,2
  Array [I] = I *. 3;
End

for i=-2,2 do
  print(array[i])
end
Lua中的多维数组:
array = {{"a","A"},{"b","B"},{"c","C"}}
for i=1,#array do
  for j=1,#array[i] do
    print(array[i][j])
  end
end

Note: pairs: traverse the table in all of the key and value
ipairs: traverse the array, according to an index from 1 increments traverse encountered nil value stops
iterated function is customizable
- iterated function will return a value, the return value will be assigned to the variable list
for the list of variables in an iterative function, state variables, control variables do
  - loop
end

function square(state,control)
  if(control>=state) then
    return nil;
  else
    control = control+1;
  return control,control*control;
  end
end

for i,j in square,9,0 do
  print(i,j);
end

Guess you like

Origin www.cnblogs.com/xingyunge/p/10936216.html