Tables as records..

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Tables as records..

Berkant Atay
Hi all,

I am new in Lua. For the last few days I've been experimenting with
some different aspects of the language such as file management,
graphics, UI etc, and amazed at the things this Plua thing can do.
Kudos Marcio!

I've been using tables as simple arrays so far. But I now need to
create and manipulate records which can hold, say, a person's name,
age, height, gender etc. This was so easy in Pascal, using "record"
type. For the last 4 hours I've been reading the manuals,
unfortunately to find that 'tables' are covered only like a
reference manual, and trying different table indexing methods on my
poor T/E. I searched the messages in our group but could not find an
answer.

Why won't the following piece of chunk work?

people={}
person={name="",age=0,male=true}

person.name="John"
person.age=30
person.male=true
people[1]=person

person.name="Mary"
person.age=25
person.male=false
people[2]=person

person.name="Frank"
person.age=12
person.male=true
people[3]=person

print(people[2].name) ---> Frank  (wrong)  ????????
gui.event()


Well, I managed to get the right answer using the assignment:
.
.
people[2]={person.name,person.age,person.male}
.
.
print(people[2][1])   ---> Mary (right)


I don't like to use indices like these ([2][1]). This is BASIC-
like.. I've even tried people={person={}} configuration, but  I
still can't get nearer to the solution. I want something like:
guy=people[2].person.name

Am I thinking too Pascal-wise?


Thanks in advance.

Reply | Threaded
Open this post in threaded view
|

Re: Tables as records..

Marcelo G Huerta
Berkant Atay escribió:

> people[1]=person

This is not a "deepcopy"; you are merely *pointing* to "person".

> person.name="Mary"
> person.age=25
> person.male=false

Here you change the existing "person"...

> people[2]=person

And here you point to the *same* person (which is also accesible thru
people[1])


[... snip ...]

> person.name="Frank"
> person.age=12
> person.male=true
> people[3]=person
>
> print(people[2].name) ---> Frank  (wrong)

Right, because all three people[] values point to the same table "person".

> Well, I managed to get the right answer using the assignment:

> people[2]={person.name,person.age,person.male}

Here you are creating *another* table on the fly, but with no names.
Doesn't matter where do you take your values from; it can be from
elements in "person", or directly constants; but the {...} is a table
constructor, and you are creating a new one, which is then pointed by
people[2].

If you did:

people[2] = {name="Mary", age=25, male=false}

you would get what you are looking for.

> Am I thinking too Pascal-wise?

Probably. It's useful to think Python-style: "person" is not a variable
but a *name* which points to a table. By referencing it you're not
copying its contents.



--

     o-=< Marcelo >=-o

Reply | Threaded
Open this post in threaded view
|

Re: Tables as records..

bpholmen
In reply to this post by Berkant Atay
Berkant,

I think Marcelo's reply to you is a good one.  For what it's worth,
here is some Lua code I wrote last year while trying to understand the
same set of questions you are asking.  A bit of experimentation
clarified it for me.  I hope this is helpful.

-Bruce

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

-- Person.lua
-- Bruce P. Holmen
-- 2006-04-03

-- ********************

_Person = {}
_Person.meta = {}
PERSON_META = "Person"

-- Person: constructor
function Person(firstName, lastName, spouse)
        -- object is a list
        local p = {}
        -- methods
        p.addSpouse = _Person.addSpouse
        p.tostring = _Person.tostring
        -- properties
        p.firstName = firstName
        p.lastName = lastName
        if (spouse) then
                p:addSpouse(spouse)
        end
        -- Person
        return p
end

-- ********************

-- Person: add a spouse
function _Person:addSpouse(firstName)
        -- one Person may contain a reference to another
        -- note how arg and self attributes are mixed
        self.spouse = Person(firstName, self.lastName)
end

-- Person: return a printable string
function _Person:tostring()
        -- build a string
        str = ""
        str = str .. self.firstName
        str = str .. " "
        str = str .. self.lastName
        -- spouse is optional
        if (self.spouse) then
                str = str .. ", Spouse = "
                str = str .. self.spouse:tostring()
        end
        -- return the string
        return str
end

-- ********************

-- make the metatable "final"
_Person.meta.__metatable = PERSON_META

-- ********************

print()
Fred = Person("Fred","Flintstone")
print("Fred = " .. Fred:tostring())
Fred:addSpouse("Wilma")
print("Fred = " .. Fred:tostring())

print()
Barney = Person("Barney","Rubble")
print("Barney = " .. Barney:tostring())
Barney:addSpouse("Betty")
print("Barney = " .. Barney:tostring())

print()
print("Fred = " .. Fred:tostring())
print("Barney = " .. Barney:tostring())

Wilbur = Person("Wilbur","Widget","Wanda","WhatEver","WhatElse")
print("Wilbur=" .. Wilbur:tostring())

-- ********************

-- eof: Person.lua

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

--- In [hidden email], "Berkant Atay" <berkant_atay@...> wrote:

>
> Hi all,
>
> I am new in Lua. For the last few days I've been experimenting with
> some different aspects of the language such as file management,
> graphics, UI etc, and amazed at the things this Plua thing can do.
> Kudos Marcio!
>
> I've been using tables as simple arrays so far. But I now need to
> create and manipulate records which can hold, say, a person's name,
> age, height, gender etc. This was so easy in Pascal, using "record"
> type. For the last 4 hours I've been reading the manuals,
> unfortunately to find that 'tables' are covered only like a
> reference manual, and trying different table indexing methods on my
> poor T/E. I searched the messages in our group but could not find an
> answer.
>
> Why won't the following piece of chunk work?
>
> people={}
> person={name="",age=0,male=true}
>
> person.name="John"
> person.age=30
> person.male=true
> people[1]=person
>
> person.name="Mary"
> person.age=25
> person.male=false
> people[2]=person
>
> person.name="Frank"
> person.age=12
> person.male=true
> people[3]=person
>
> print(people[2].name) ---> Frank  (wrong)  ????????
> gui.event()
>
>
> Well, I managed to get the right answer using the assignment:
> .
> .
> people[2]={person.name,person.age,person.male}
> .
> .
> print(people[2][1])   ---> Mary (right)
>
>
> I don't like to use indices like these ([2][1]). This is BASIC-
> like.. I've even tried people={person={}} configuration, but  I
> still can't get nearer to the solution. I want something like:
> guy=people[2].person.name
>
> Am I thinking too Pascal-wise?
>
>
> Thanks in advance.
>


Reply | Threaded
Open this post in threaded view
|

Re: Re: Tables as records..

Berkant Atay
In reply to this post by Berkant Atay

Hey,
Thank you for the replies. Marcelo's reply has been very helpful. Bruce just answered while I was looking at some complex applications of tables, such as methods, meta stuff and oop. I am a bit confused since no pointers and type definitions are allowed in Lua like conventionals systems. But I decided to take a look at some of the sample codes in lua.org along with the reference and programming guide. This method has always worked for C and Pascal for me in the past.
By the way, when and where do you write Plua? I find it very comfortable to write on my Treo in the bed, usually just after my wife falls asleep. A few days ago she told me that she suspected that I found a way to communicate with some female using the Treo :)

-----Original Message-----

From: "bpholmen" <[hidden email]>
Subj: [plua] Re: Tables as records..
Date: Thu 22 Feb 2007 19:45
Size: 16K
To: [hidden email]

Berkant,

I think Marcelo's reply to you is a good one. For what it's worth,
here is some Lua code I wrote last year while trying to understand the
same set of questions you are asking. A bit of experimentation
clarified it for me. I hope this is helpful.

-Bruce

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

-- Person.lua
-- Bruce P. Holmen
-- 2006-04-03

-- ********************

_Person = {}
_Person.meta = {}
PERSON_META = "Person"

-- Person: constructor
function Person(firstName, lastName, spouse)
-- object is a list
local p = {}
-- methods
p.addSpouse = _Person.addSpouse
p.tostring = _Person.tostring
-- properties
p.firstName = firstName
p.lastName = lastName
if (spouse) then
p:addSpouse(spouse)
end
-- Person
return p
end

-- ********************

-- Person: add a spouse
function _Person:addSpouse(firstName)
-- one Person may contain a reference to another
-- note how arg and self attributes are mixed
self.spouse = Person(firstName, self.lastName)
end

-- Person: return a printable string
function _Person:tostring()
-- build a string
str = ""
str = str .. self.firstName
str = str .. " "
str = str .. self.lastName
-- spouse is optional
if (self.spouse) then
str = str .. ", Spouse = "
str = str .. self.spouse:tostring()
end
-- return the string
return str
end

-- ********************

-- make the metatable "final"
_Person.meta.__metatable = PERSON_META

-- ********************

print()
Fred = Person("Fred","Flintstone")
print("Fred = " .. Fred:tostring())
Fred:addSpouse("Wilma")
print("Fred = " .. Fred:tostring())

print()
Barney = Person("Barney","Rubble")
print("Barney = " .. Barney:tostring())
Barney:addSpouse("Betty")
print("Barney = " .. Barney:tostring())

print()
print("Fred = " .. Fred:tostring())
print("Barney = " .. Barney:tostring())

Wilbur = Person("Wilbur","Widget","Wanda","WhatEver","WhatElse")
print("Wilbur=" .. Wilbur:tostring())

-- ********************

-- eof: Person.lua

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

--- In [hidden email], "Berkant Atay" <berkant_atay@...> wrote:
>
> Hi all,
>
> I am new in Lua. For the last few days I've been experimenting with
> some different aspects of the language such as file management,
> graphics, UI etc, and amazed at the things this Plua thing can do.
> Kudos Marcio!
>
> I've been using tables as simple arrays so far. But I now need to
> create and manipulate records which can hold, say, a person's name,
> age, height, gender etc. This was so easy in Pascal, using "record"
> type. For the last 4 hours I've been reading the manuals,
> unfortunately to find that 'tables' are covered only like a
> reference manual, and trying different table indexing methods on my
> poor T/E. I searched the messages in our group but could not find an
> answer.
>
> Why won't the following piece of chunk work?
>
> people={}
> person={name="",age=0,male=true}
>
> person.name="John"
> person.age=30
> person.male=true
> people[1]=person
>
> person.name="Mary"
> person.age=25
> person.male=false
> people[2]=person
>
> person.name="Frank"
> person.age=12
> person.male=true
> people[3]=person
>
> print(people[2].name) ---> Frank (wrong) ????????
> gui.event()
>
>
> Well, I managed to get the right answer using the assignment:
> .
> .
> people[2]={person.name,person.age,person.male}
> .
> .
> print(people[2][1]) ---> Mary (right)
>
>
> I don't like to use indices like these ([2][1]). This is BASIC-
> like.. I've even tried people={person={}} configuration, but I
> still can't get nearer to the solution. I want something like:
> guy=people[2].person.name
>
> Am I thinking too Pascal-wise?
>
>
> Thanks in advance.
>





__._,_.___
Recent Activity
Visit Your Group
SPONSORED LINKS

Film it. Change the world.

Need traffic?

Drive customers

With search ads

on Yahoo!

Y! Toolbar

Get it Free!

easy 1-click access

to your groups.

Yahoo! Groups

Start a group

in 3 easy steps.

Connect with others.

.

__,_._,___