%pop in lua?

Post Reply
juraj
Posts: 15
Joined: Wed Jan 13, 2010 2:47 pm

%pop in lua?

Post by juraj »

Is there a function similar to zMud's %pop function in Lua. I'd like to do something like:

Code: Select all

>> queue = {"do this", "do that", "do something else"}
>>send(table.pop( queue))
do this
>> for i,v in ipairs(queue) do print(i,v) end
1   do that
2   do the other thing
I realise that I could do this by removing items with table.remove(), but if I can keep it cleaner looking and expand my syntax vocabulary, then I intend to.

User avatar
Vadi
Posts: 5042
Joined: Sat Mar 14, 2009 3:13 pm

Re: %pop in lua?

Post by Vadi »

I'm afraid I didn't catch you.

table.remove is the native lua name, table.pop is probably a zmud alias that calls table.remove. If you'd like you can make a table.pop that will call table.remove, but it's really more healthy to stick to pure Lua.

juraj
Posts: 15
Joined: Wed Jan 13, 2010 2:47 pm

Re: %pop in lua?

Post by juraj »

I just made up table.pop as an example. I'm looking for a way to return the first item of an array and then immediately remove it from the array, but I can do it in multiple steps as well.

User avatar
demonnic
Posts: 886
Joined: Sat Dec 05, 2009 3:19 pm

Re: %pop in lua?

Post by demonnic »

next_action = table.remove(action_queue, 1) will pull the item out of the table action_queue at index 1 and assign it to the variable next_action. Is this what you're looking for?

User avatar
Vadi
Posts: 5042
Joined: Sat Mar 14, 2009 3:13 pm

Re: %pop in lua?

Post by Vadi »

Just keep in mind that key-pair tables in Lua don't preserve order. If you'd like to keep it, use the "php-like" tables available from here: http://lua-users.org/wiki/MakingLuaLikePhp

davidk
Posts: 13
Joined: Wed Dec 09, 2009 7:01 pm
Location: avalon.mud.de

Re: %pop in lua?

Post by davidk »

Vadi wrote:Just keep in mind that key-pair tables in Lua don't preserve order. If you'd like to keep it, use the "php-like" tables available from here: http://lua-users.org/wiki/MakingLuaLikePhp
In the example in the original post integer keys were used so the order is explicitly the order of those numbers.
table.remove works on those integer keys and shifts the higher numbers down, so "x = table.remove(t, 1)" would work as intended.

juraj
Posts: 15
Joined: Wed Jan 13, 2010 2:47 pm

Re: %pop in lua?

Post by juraj »

Perfect! Thank you

Post Reply