Some questions about the use of plua

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

Some questions about the use of plua

mj_vandervelden
Hi, I recently finished a plua program to handle anniveraries,
birthdays etc. I tried it in other languages, but plua is clearly a
great product with an easy to use GUI system. While programming I ran
into the following questions:

1. Instead of T = {} I use

while table.getn(t) > 0 do
  table.remove(t)
end

Is there any difference in terms of garbage collection?

2. I could not use a method as parameter for the sort function:
table.sort(self, Cmp) = OK
table.sort(self, self:Cmp) = NOT OK

3. On my treo650 the menu is not displayed after auto power-off

4. Pressing up and down arrow in the menu does not seem to work

5. I was not able to open a file in such a way that if it exists it
is first made empty. I tried to open "w", "wb", "w+" but it seems
that I did not use the correct form. As a workaround I do the
following:
while n > 0 do
  db:deleterec(n - 1)
  n = n - 1
end

This works fine, but are there any suggesion?

6. Is there a function to remove a file?

regards,
Mark





 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/plua/

<*> To unsubscribe from this group, send an email to:
    [hidden email]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 


Reply | Threaded
Open this post in threaded view
|

Re: Some questions about the use of plua

Stefan Vorkoetter
mj_vandervelden wrote:

> Hi, I recently finished a plua program to handle anniveraries,
> birthdays etc. I tried it in other languages, but plua is clearly a
> great product with an easy to use GUI system. While programming I ran
> into the following questions:
>
> 1. Instead of T = {} I use
>
> while table.getn(t) > 0 do
>   table.remove(t)
> end
>
> Is there any difference in terms of garbage collection?

I'm not expert on Plua's internals, but do have familiarity with the implementation of Plua-like languages. I suspect that what you're doing is horrendously inefficient compared to the much simpler T = {}. Also, your loop only removes numeric-indexed entries in T.

Stefan


 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/plua/

<*> To unsubscribe from this group, send an email to:
    [hidden email]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 


Reply | Threaded
Open this post in threaded view
|

Re: Some questions about the use of plua

Doug Rogers-4
In reply to this post by mj_vandervelden
mj_vandervelden wrote:

> 1. Instead of T = {} I use
> while table.getn(t) > 0 do
>   table.remove(t)
> end
> Is there any difference in terms of garbage collection?

Like Stefan, I would suspect that the former method is MUCH better, but
I have done no tests on my palm pilot.

> 2. I could not use a method as parameter for the sort function:
> table.sort(self, Cmp) = OK
> table.sort(self, self:Cmp) = NOT OK

Try self.Cmp (dot, not colon) in your list object. The colon is only
used in defining and invoking function calls.

I normally put comparison functions directly in the object, but putting
them in the list might make more sense. I've got a short example of how
I do it below.

I can't help with your other questions.

Here's the output from running "lua -e "dofile'sorttest.lua'" under Linux:

Initial:
1       {name='a',value=5}
2       {name='b',value=3}
After sort:
1       {name='b',value=3}
2       {name='a',value=5}
After changing b.value:
1       {name='a',value=5}
2       {name='b',value=8}

-- sorttest.lua
Object = {}

function Object:new(name, value)
   object = { name = name or 'unnamed', value = value or 0 }
   setmetatable(object, self.metatable)
   return object
end

function Object:compare(other)
   return self.value - other.value
end

function Object:tostring()
   return string.format("{name='%s',value=%d}", self.name, self.value)
end

Object.metatable = { __index = Object, __tostring = Object.tostring }

a = Object:new('a', 5)
b = Object:new('b', 3)
list = { a, b }
print('Initial:')
table.foreach(list, print)
table.sort(list, Object.compare)
print('After sort:')
table.foreach(list, print)
b.value = 8
table.sort(list, Object.compare)
print('After changing b.value:')
table.foreach(list, print)


--
--__-__-____------_--_-_-_-___-___-____-_--_-___--____
Doug Rogers - ICI - V:703.893.2007x220 www.innocon.com
-_-_--_------____-_-_-___-_--___-_-___-_-_---_--_-__-_


 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/plua/

<*> To unsubscribe from this group, send an email to:
    [hidden email]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 


Reply | Threaded
Open this post in threaded view
|

Re: Some questions about the use of plua

mj_vandervelden
Thanks Doug (and Stefan) for helping me with understanding more of
Plua. I can clearly clean up my code. Actually I had no experience
with object oriented programming in Plua, but the example below is of
great help.

Appriciated!!

regards
Mark

--- In [hidden email], Doug Rogers <rogers@...> wrote:

>
> mj_vandervelden wrote:
>
> > 1. Instead of T = {} I use
> > while table.getn(t) > 0 do
> >   table.remove(t)
> > end
> > Is there any difference in terms of garbage collection?
>
> Like Stefan, I would suspect that the former method is MUCH better,
but

> I have done no tests on my palm pilot.
>
> > 2. I could not use a method as parameter for the sort function:
> > table.sort(self, Cmp) = OK
> > table.sort(self, self:Cmp) = NOT OK
>
> Try self.Cmp (dot, not colon) in your list object. The colon is only
> used in defining and invoking function calls.
>
> I normally put comparison functions directly in the object, but
putting
> them in the list might make more sense. I've got a short example of
how
> I do it below.
>
> I can't help with your other questions.
>
> Here's the output from running "lua -e "dofile'sorttest.lua'" under
Linux:

>
> Initial:
> 1       {name='a',value=5}
> 2       {name='b',value=3}
> After sort:
> 1       {name='b',value=3}
> 2       {name='a',value=5}
> After changing b.value:
> 1       {name='a',value=5}
> 2       {name='b',value=8}
>
> -- sorttest.lua
> Object = {}
>
> function Object:new(name, value)
>    object = { name = name or 'unnamed', value = value or 0 }
>    setmetatable(object, self.metatable)
>    return object
> end
>
> function Object:compare(other)
>    return self.value - other.value
> end
>
> function Object:tostring()
>    return string.format("{name='%s',value=%d}", self.name,
self.value)
> end
>
> Object.metatable = { __index = Object, __tostring =
Object.tostring }

>
> a = Object:new('a', 5)
> b = Object:new('b', 3)
> list = { a, b }
> print('Initial:')
> table.foreach(list, print)
> table.sort(list, Object.compare)
> print('After sort:')
> table.foreach(list, print)
> b.value = 8
> table.sort(list, Object.compare)
> print('After changing b.value:')
> table.foreach(list, print)
>
>
> --
> --__-__-____------_--_-_-_-___-___-____-_--_-___--____
> Doug Rogers - ICI - V:703.893.2007x220 www.innocon.com
> -_-_--_------____-_-_-___-_--___-_-___-_-_---_--_-__-_
>







 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/plua/

<*> To unsubscribe from this group, send an email to:
    [hidden email]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/