[Ilugc] lua tables: initial cut intro

  • From: vatsala.lists@xxxxxxxxx (Vatsala Dorairajan)
  • Date: Mon, 07 Jan 2013 14:18:31 +0530

Thanks for these bite-sized lessons Girish, makes it easier for one to 
get on-board with Lua. Looking forward to the next write up in this series
Vatsala
On Monday 07 January 2013 12:49 PM, Girish Venkatachalam wrote:

If you understand lua tables then you know lua.

So obviously we are not there yet.

Just like perl implements modules, packages and namespaces using the
OO and reference concept,
lua does everything using tables or dictionaries or hashes or
associative arrays.

All are same.

A table is a name value pair. Not just in lua but in everything.

We have lookup tables even in hardware.

In programming a hash table is very popular. One key and one value.
The value can be a reference to
  an aggregate data structure like arrays or even other tables.

If you have been with me so far(I covered a lot of ground in this
intro) then good.

Otherwise don't worry. You can learn as you go along.

Let us look at a simple lua table.

Just type lua like python. Both have interactive shells.

$ lua
Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio
tablex={2,3,4,5,100}
table.foreach(tablex,print)
1       2
2       3
3       4
4       5
5       100
tabhash={name="Girish", job="Software engineer", hobby="LUG"}
table.foreach(tabhash,print)
hobby   LUG
name    Girish
job     Software engineer
You see the idea?

You can type this code also.

$ cat learntable
#!/usr/local/bin/lua

t={2,3, 5, name="LUG", age=10}

for k,v in pairs(t) do
         print(k,v)
end


We have learnt a few iterator constructs in lua for printing all the
elements in a table.

It is a composite data type like all scripting languages.

A table can contain anything. It does not care about data type.

-Girish


Other related posts: