lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> array = {a,b}

Unless a and b are defined, this is equivalent to:
  array = {nil, nil}
which is equivalent to
  array = {}

> array["a"] = {}

You could write
  array.a = {}

> array["b"] ={}

You could actually have written:

  array = {a = {}, b = {}}

which is probably what you meant by your first line.

> array["a"]["a"] = {1}
> array["a"]["b"] = {1}

You could write:

  array.a.b = {1}

But I suspect you meant:

  array.a.b = 1

> array["b"]["a"] = {2}
> array["b"]["b"] = {2}

See above.

Perhaps you intended a and b to be values, rather than string constants.
Try this: (lots of inefficiencies left as an exercise)

function combarray(n)
  -- create a 1x1 0-indexed array such that a[0][0] is 1
  local array = {[0] = {[0] = 1}}
  -- now fill in non-zero elements
  for i = 1, n do
    -- avoid referencing undefined elements
    array[i] = {[0] = 1, [i] = 1}
    for j = 1, i - 1 do
      array[i][j] = array[i - 1][j - 1] + array[i - 1][j]
    end
  end
  return array
end

combs = combarray(16)
for i = 0, 16 do
  local line = tostring(combs[i][0]) 
  for j = 1, i do
    line = line .. " " .. tostring(combs[i][j])
  end
  line = string.rep(" ", math.floor((79 - string.len(line)) / 2)) .. line
  print(line)
end