[Ilugc] lua again

  • From: girishvenkatachalam@xxxxxxxxx (Girish Venkatachalam)
  • Date: Sat, 2 Feb 2013 09:48:13 +0530

Some more tables.


%cat tex.lua
person = {}
person["First"] = {
        name = "Girish here",
        birthyear = 1906,
        deathyear = 1978,
}
person["Second"] = {
        name = "Some other Name",
        birthyear = 1898 ,
        deathyear = 1972,
}

for variable, person in pairs(person) do
        print( variable, person.name )
        print( variable, person.birthyear )
        print( variable, person.deathyear )
        print()
end

print("Now sequential..")

table.sort(person)

for variable, person in pairs(person) do
        print( variable, person.name )
        print( variable, person.birthyear )
        print( variable, person.deathyear )
        print()
end

person = {}
person[3] = {
        name = "Girish here",
        birthyear = 1906,
        deathyear = 1978,
}
person[1] = {
        name = "Some other Name",
        birthyear = 1898 ,
        deathyear = 1972,
}

table.sort(person)

for variable, person in pairs(person) do
        print( variable, person.name )
        print( variable, person.birthyear )
        print( variable, person.deathyear )
        print()
end

Now for output:
%lua tex.lua
Second  Some other Name
Second  1898
Second  1972

First   Girish here
First   1906
First   1978

Now sequential..
Second  Some other Name
Second  1898
Second  1972

First   Girish here
First   1906
First   1978

1       Some other Name
1       1898
1       1972

3       Girish here
3       1906
3       1978

----------------------------

We see that the function table.sort() has no effect in the previous case. Why?

It can only sot tables with numeric indices. It does not do string sorting.

And the other statements must be familiar. We define two tables and
these two tables
 are contained within another table. So it is multi dimensional tables.

The pairs() function takes a table and returns the key and value.

First we obtain the key and value for the global table person{}  and then
 we look at the two tables, person["First"] and person["Second"]

-Girish

-- 
Gayatri Hitech
http://gayatri-hitech.com

Other related posts:

  • » [Ilugc] lua again - Girish Venkatachalam