newbie - try catch - array of struct - gc - loop terminated

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

newbie - try catch - array of struct - gc - loop terminated

Diego - Red Baires

Hi everyone, my name is Diego and i am a newbie trying to
learn LUA. the following are some questions because i couldnt
f¡nd information about it. could you help me pls ?

1) does LUA accept some kind of try/catch structures ? any example ?

2) will GC free this NIL assignments ?

t = {}
i = 10;
t[1] = "diego";
t[2] = "martin";

-- do something...

t = nil;
i = nil;
collectgarbage("collect")

3) is there a way to create an array of a structured type ? something like this

mytype = struct {
  s : string;
  i : int;
}

var myarray : mytype;

myarray[1].s = "diego";
myarray[1].i = 10;
myarray[2].s = "martin";
myarray[2].i = 2;


4) suppose this scenario: i run a LUA script inside a process.
the process receives a finish-application message.
does it exists any "terminated" flag to know this "terminating process"

i mean, how do i have to do to exit an infinit loop ?

while not(lua.terminating)
  -- do loop
end



thanks in advance,
diego





What can you do with the new Windows Live? Find out
Reply | Threaded
Open this post in threaded view
|

Re: newbie - try catch - array of struct - gc - loop terminated

Adrian Sietsma
Diego - Red Baires wrote:
>
> Hi everyone, my name is Diego and i am a newbie trying to
> learn LUA. the following are some questions because i couldnt
> f¡nd information about it. could you help me pls ?
>
> 1) does LUA accept some kind of try/catch structures ? any example ?

you could use pcall (it's in the manual) : I use it to invoke user-supplied
"callback" routines.

>
> 2) will GC free this NIL assignments ?
>
> t = {}
> i = 10;
> t[1] = "diego";
> t[2] = "martin";
>
> -- do something...
>
> t = nil;
> i = nil;
> collectgarbage("collect")
>
Yes

> 3) is there a way to create an array of a structured type ? something
> like this
>
> mytype = struct {
>   s : string;
>   i : int;
> }
>
> var myarray : mytype;
>
> myarray [1].s = "diego";
> myarray [1].i = 10;
> myarray [2].s = "martin";
> myarray [2].i = 2;
>
>
There are many OO frameworks around for Lua involving metatables, but as the
simplest case :

local myarray = {}
myarray[1] = {s="diego",i=10}
myarray[2] = {s="martin",i=2}



> 4) suppose this scenario: i run a LUA script inside a process.
> the process receives a finish-application message.
> does it exists any "terminated" flag to know this "terminating process"
>
> i mean, how do i have to do to exit an infinit loop ?
>
> while not(lua.terminating)
>   -- do loop
> end

I'll leave that for someone else.

Adrian
Reply | Threaded
Open this post in threaded view
|

Re: newbie - try catch - array of struct - gc - loop terminated

Ico Doornekamp

* On 2009-04-29 Adrian Sietsma <[hidden email]> wrote  :

>> 4) suppose this scenario: i run a LUA script inside a process.
>> the process receives a finish-application message.
>> does it exists any "terminated" flag to know this "terminating process"
>>
>> i mean, how do i have to do to exit an infinit loop ?
>>
>> while not(lua.terminating)
>>   -- do loop
>> end
>
> I'll leave that for someone else.

I can only comment on this from the Unix/POSIX point of view, since I am
completely ignorant of programming in the win32 environment.

In POSIX, a running process can be killed by sending it a signal - this
is done with the kill command or the kill() function, or by hitting
Ctrl-C in an interactive shell. (For simplicity I will only talk about
the 'TERM' signal, which is usually used to request the termination of a
process)

The program that receives the signal has a few ways to deal with this:

The default is to do nothing special at all. In this case, the operating
system decides to terminate the program, which means that it just stops
running right in the middle of whatever it happened to be doing, all its
memory is freed, its file descriptors are closed: it simply ceases to
exist. This can be ok for your program, but if you want to make sure
your code can cleanup and exit gracefully, this is probably not
sufficient.

Another method would be to request the operating system to call a given
function when the application receives the TERM signal. This function
can then indicate your inifinite loop that the process is requested to
exit, after which you can do a proper cleanup and terminate the program.

The problem in your case might be that Lua has no native support for
working with signals, e.g, there is no way to register a Lua function as
a signal handler. This is because Lua is mostly oblivious about
OS-specific things like processes and signals. This sounds like a bad
thing, but it is one of the reasons why lua is so small and lean, and
portable to virtually any platform with support for a C compiler and
enough memory avaialble.

So these are your options:

- Do not rely on signals at all, but provide another mechanism for
  instructing your program to exit. (network, command line, etc ?)

- Add some code written in C to your program which does the signal
  handling. The simplest implementation would simply set a global flag
  when the signal is received, and your mainloop can poll the value
  of this flag through a simple C function.

- Learn to live with the program termination. This is really not a big
  problem for a lot of applications.


Ico

--
:wq
^X^Cy^K^X^C^C^C^C
Reply | Threaded
Open this post in threaded view
|

Re: newbie - try catch - array of struct - gc - loop terminated

Luiz Henrique de Figueiredo
> The problem in your case might be that Lua has no native support for
> working with signals

But you can do it in C. The Lua standalone interpreter handles SIGINT signals
by setting up hooks to abort the running Lua program gracefully. See
        http://www.lua.org/source/5.1/lua.c.html#docall
Reply | Threaded
Open this post in threaded view
|

Re: newbie - try catch - array of struct - gc - loop terminated

Philippe Lhoste
In reply to this post by Diego - Red Baires
On 29/04/2009 09:23, Diego - Red Baires wrote:
> learn LUA. [...]
> 1) does LUA accept some kind of try/catch structures ? any example ?

That's funny, I never saw written PERL or JAVA, but we often see LUA instead of Lua.
What makes people think it is an acronym? Is that bad influence from C, PHP, VB, or even
BASIC or FORTRAN? :-) (the latter is actually made of two words, but I often saw it
written in capitals).

(Someone has to point that out! ;-))

--
Philippe Lhoste
--  (near) Paris -- France
--  http://Phi.Lho.free.fr
--  --  --  --  --  --  --  --  --  --  --  --  --  --

Reply | Threaded
Open this post in threaded view
|

RE: newbie - try catch - array of struct - gc - loop terminated

Diego - Red Baires
In reply to this post by Luiz Henrique de Figueiredo

Hi guys,
thank you all, for taking time to help me.

i am not embedding Lua into my own C++ development.
i am using Lua because it is the scripting used on an updater-software called
IndigoRose's TrueUpdate.
(trueupdate is the container c++ software that integrates Lua)

now i want to script a loop.
but what happend if while looping, the user press "shut down computer" ?

i need to detect the broadcasted FINISH win_message, so i can exit the loop
and not "hanging" the updater.

i am asking for this propietary flag to indigorose's guys, but i thought
that may be Lua has something to break the script.

thanks again,
diego



>
> > The problem in your case might be that Lua has no native support for
> > working with signals
>
> But you can do it in C. The Lua standalone interpreter handles SIGINT signals
> by setting up hooks to abort the running Lua program gracefully. See
> http://www.lua.org/source/5.1/lua.c.html#docall


Invite your mail contacts to join your friends list with Windows Live Spaces. It's easy! Try it!
Reply | Threaded
Open this post in threaded view
|

Re: newbie - try catch - array of struct - gc - loop terminated

Luiz Henrique de Figueiredo
> now i want to script a loop.
> but what happend if while looping, the user press "shut down computer" ?
>
> i need to detect the broadcasted FINISH win_message, so i can exit the loop
> and not "hanging" the updater.
>
> i am asking for this propietary flag to indigorose's guys, but i thought
> that may be Lua has something to break the script.

If TrueUpdate does not catch signals or export an API for that, then
it cannot be done in pure Lua.
Reply | Threaded
Open this post in threaded view
|

rare sorting on a table

Diego - Red Baires

Hi guys, i have this structure and i am getting a strange result. 
could anyone explain me why ?

PAUTAS = {
  [0] = {
    ID_PAUTA  = "GUILLE",
    REQ_FILES = {
  [0] = {FILENAME = "0-FILE1", MD5 = "0-MD5-1"},
      [1] = {FILENAME = "0-FILE2", MD5 = "0-MD5-2"},
  }
  },
  [1] = {
    ID_PAUTA  = "LICH",
    REQ_FILES = {
  [0] = {FILENAME = "1-FILE1", MD5 = "1-MD5-1"},
      [1] = {FILENAME = "1-FILE2", MD5 = "1-MD5-2"},
      [2] = {FILENAME = "1-FILE3", MD5 = "1-MD5-3"},
  }
  },
}

for i, pauta in pairs(PAUTAS) do
  print (i.."> "..pauta.ID_PAUTA);
  for k, req_file in pairs(pauta.REQ_FILES) do
    print(k.."  > "..req_file.FILENAME.." ("..req_file.MD5..")")
  end
end


i supposed to get this result:

0> GUILLE
0  > 0-FILE1 (0-MD5-1)
1  > 0-FILE2 (0-MD5-2)
1> LICH
0  > 1-FILE1 (1-MD5-1)
1  > 1-FILE2 (1-MD5-2)
2  > 1-FILE3 (1-MD5-3)

---------- BUT INSTEAD OF THAT, I ALWAYS GET THIS SORT (SWAP BETWEEN 2,1)

0> GUILLE
0  > 0-FILE1 (0-MD5-1)
1  > 0-FILE2 (0-MD5-2)
1> LICH
0  > 1-FILE1 (1-MD5-1)
2  > 1-FILE3 (1-MD5-3)
1  > 1-FILE2 (1-MD5-2)


ANY IDEA ?

thanks







Windows Live™: Keep your life in sync. Check it out!
Reply | Threaded
Open this post in threaded view
|

Re: rare sorting on a table

Wim Couwenberg
> for i, pauta in pairs(PAUTAS) do
>   print (i.."> "..pauta.ID_PAUTA);
>   for k, req_file in pairs(pauta.REQ_FILES) do
>     print(k.."  > "..req_file.FILENAME.." ("..req_file.MD5..")")
>   end
> end

use ipairs instead of pairs.

bye,
Wim
Reply | Threaded
Open this post in threaded view
|

Re: rare sorting on a table

Javier Guerra Giraldez
In reply to this post by Diego - Red Baires
pairs() don't guarantee the order.  use ipairs() for that

--
Javier
Reply | Threaded
Open this post in threaded view
|

Re: rare sorting on a table

Doug Rogers
In reply to this post by Wim Couwenberg
Wim Couwenberg wrote:
>> for i, pauta in pairs(PAUTAS) do
>>   print (i.."> "..pauta.ID_PAUTA);
>>   for k, req_file in pairs(pauta.REQ_FILES) do
>>     print(k.."  > "..req_file.FILENAME.." ("..req_file.MD5..")")
>>   end
>> end
>
> use ipairs instead of pairs.

Well, since you're using 0, this won't work. Array indexes start at 1 in
Lua. A 0 is allowed as an index, but it's a normal table index, not an
array index, so it will not be included in ipairs().

Doug


______________________________________________________________________________________
The information contained in this email transmission may contain proprietary and business
sensitive information.  If you are not the intended recipient, you are hereby notified that
any review, dissemination, distribution or duplication of this communication is strictly
prohibited.  Unauthorized interception of this e-mail is a violation of law.  If you are not
the intended recipient, please contact the sender by reply email and immediately destroy all
copies of the original message.

Any technical data and/or information provided with or in this email may be subject to U.S.
export controls law.  Export, diversion or disclosure contrary to U.S. law is prohibited.  
Such technical data or information is not to be exported from the U.S. or given to any foreign
person in the U.S. without prior written authorization of Elbit Systems of America and the
appropriate U.S. Government agency.
Reply | Threaded
Open this post in threaded view
|

RE: rare sorting on a table

Diego - Red Baires

thanks everyone.

>> use ipairs instead of pairs.
>
> Well, since you're using 0, this won't work. Array indexes start at 1 in
> Lua. A 0 is allowed as an index, but it's a normal table index, not an
> array index, so it will not be included in ipairs().




See all the ways you can stay connected to friends and family
Reply | Threaded
Open this post in threaded view
|

how to compare 2 dates

Diego - Red Baires
In reply to this post by Doug Rogers

hi again guys,

i cant find information regarding the comparision of dates and times.

myDate = "05/21/2009";

if ( os.date()> myDate) then
  print "myDate is a previuos date";

if ( os.date() = myDate) then
  print "myDate is today";

if ( os.date() < myDate) then
  print "myDate is in the future";




is this correct ?
will this work to all locales ?
what about comparing times ? (i mean, if a time is between two values)

thanks one more time



Windows Live™: Keep your life in sync. Check it out!
Reply | Threaded
Open this post in threaded view
|

Re: how to compare 2 dates

Robert Raschke

On Wed, May 6, 2009 at 6:33 PM, Diego - Red Baires <[hidden email]> wrote:

hi again guys,

i cant find information regarding the comparision of dates and times.

myDate = "05/21/2009";

if ( os.date()> myDate) then
  print "myDate is a previuos date";

if ( os.date() = myDate) then
  print "myDate is today";

if ( os.date() < myDate) then
  print "myDate is in the future";




is this correct ?
will this work to all locales ?
what about comparing times ? (i mean, if a time is between two values)

thanks one more time

No that won't work. Dates are tricky to handle. And it's not good to treat them as strings.

Have a look at the luadate library: http://luaforge.net/projects/date/

Robby
 

Reply | Threaded
Open this post in threaded view
|

Re: how to compare 2 dates

Yuri Takhteyev
In reply to this post by Diego - Red Baires
If your date is represented in the yyyy/mm/dd format, instead of
dd/mm/yyyy, you can just compare it with os.date("%Y/%m/%d").

if (os.date("%Y/%m/%d") < myDate) then
   print "myDate is the future";
end

 - yuri

On Wed, May 6, 2009 at 10:33 AM, Diego - Red Baires
<[hidden email]> wrote:

>
> hi again guys,
> i cant find information regarding the comparision of dates and times.
> myDate = "05/21/2009";
> if ( os.date()> myDate) then
>   print "myDate is a previuos date";
> if ( os.date() = myDate) then
>   print "myDate is today";
> if ( os.date() < myDate) then
>   print "myDate is in the future";
>
>
>
> is this correct ?
> will this work to all locales ?
> what about comparing times ? (i mean, if a time is between two values)
> thanks one more time
>
> ________________________________
> Windows Live™: Keep your life in sync. Check it out!



--
http://spu.tnik.org/
Reply | Threaded
Open this post in threaded view
|

Re: how to compare 2 dates

Doug Rogers
In reply to this post by Diego - Red Baires
Diego - Red Baires wrote:
> myDate = "05/21/2009";
> ...
> if ( os.date() < myDate) then
>   print "myDate is in the future";
> is this correct ?
> will this work to all locales ?
> what about comparing times ? (i mean, if a time is between two values)

Diego, your best bet is to convert the date into a time. You'll need to
extract the month, day, and year, then pass that into os.time().

function date2time(s)
    local m, d, y = s:match("(%d+)/(%d+)/(%d+)")
    if not y then
       return nil, "improper date format"
    end
    return os.time({ year = y, month = m, day = d })
end

t = assert(date2time("05/21/2009"))

Then you may compare these values.

Doug

______________________________________________________________________________________
The information contained in this email transmission may contain proprietary and business
sensitive information.  If you are not the intended recipient, you are hereby notified that
any review, dissemination, distribution or duplication of this communication is strictly
prohibited.  Unauthorized interception of this e-mail is a violation of law.  If you are not
the intended recipient, please contact the sender by reply email and immediately destroy all
copies of the original message.

Any technical data and/or information provided with or in this email may be subject to U.S.
export controls law.  Export, diversion or disclosure contrary to U.S. law is prohibited.  
Such technical data or information is not to be exported from the U.S. or given to any foreign
person in the U.S. without prior written authorization of Elbit Systems of America and the
appropriate U.S. Government agency.
Reply | Threaded
Open this post in threaded view
|

Re: how to compare 2 dates

Petite Abeille-2-2
In reply to this post by Diego - Red Baires

On May 6, 2009, at 7:33 PM, Diego - Red Baires wrote:

> is this correct ?will this work to all locales ?what about comparing  
> times ? (i mean, if a time is between two values)

As mentioned by Robert, this is not going to fly very far as you would  
be comparing two random strings... you could use a more comparison  
friendly string representation such as ISO 8601 or something, e.g.  
1997-07-16T19:20:30.45... but you will be better off using a numerical  
representation such as the one returned by os.time():

http://www.lua.org/manual/5.1/manual.html#pdf-os.time



Reply | Threaded
Open this post in threaded view
|

RE: how to compare 2 dates

Diego - Red Baires

many thanks to all of you.

> As mentioned by Robert, this is not going to fly very far as you would




check out the rest of the Windows Live™. More than mail–Windows Live™ goes way beyond your inbox. More than messages
Reply | Threaded
Open this post in threaded view
|

RE: how to compare 2 dates - NOW WITH TIME

Diego - Red Baires
In reply to this post by Petite Abeille-2-2

Hi guys, after reading your comments i finally choose this solution
(i first selected comparing strings, but "2009/13/25" is a "valid" input, so i dropped it)

premise:

i need to represent a range or dates, and then a range of time.
i have a list of records that indicates that an event must be
executed only if:
a) today is between the fields that indicates a range of dates, and then
b) os system.clock is between the fields that indicates a range of hours.

(is today between date-A and date-B, if so, is now between 3pm and 4pm?)

so i am doing this:

-----------
between_date_from = os.time({ year="2008", month="01", day="01", hour="00", min="00" })
between_date_to   = os.time({ year="2009", month="05", day="06", hour="00", min="00" })

today = os.time()

if (today> between_date_from) and (today < (between_date_to+86400)) then
  print "today IS between the range of dates"
else
  print "today is NOT between the range of dates"
end
-----------

now the problem is how to analize a range of hours, not taking in mind the date.
any idea ?

"os.time()" does not allow (obviously) an assignation like this:
{ year="0000", month="00", day="00", hour="17", min="30" }


thanks
diego







Windows Live™: Keep your life in sync. Check it out!
Reply | Threaded
Open this post in threaded view
|

Re: how to compare 2 dates - NOW WITH TIME

Javier Guerra Giraldez
On Thu, May 7, 2009 at 10:39 AM, Diego - Red Baires
<[hidden email]> wrote:

> (is today between date-A and date-B, if so, is now between 3pm and 4pm?)

much better and easier is:

is now betwenn date-A/3pm and dateB-4pm?

for this, convert the full date/time to a timestamp and compare 'now' with those

--
Javier
12