Home
Reading
Searching
Subscribe
Sponsors
Statistics
Posting
Contact
Spam
Lists
Links
About
Hosting
Filtering
Features Download
Marketing
Archives
FAQ
Blog
 
Gmane
From: Shmuel Zeigerman <shmuz <at> bezeqint.net>
Subject: Re: Extract a list of keys from table
Newsgroups: gmane.comp.lang.lua.general
Date: Thursday 17th July 2008 22:32:40 UTC (over 9 years ago)
Alexander Gladysh wrote:
> print(
>     extract_keys(
>         { a = 1, b = 2, c = 3 },
>         "a", "b", "xxx", "c", "yyy"
>       )
>   )
>  --> 1        2        nil         3        nil

An option is using recursive functions:

-- "mapper" is a table:
function map(tab, ...)
   if select("#", ...) > 0 then
     return tab[...], map(tab, select(2,...))
   end
end

-- "mapper" is a function:
function map(func, ...)
   if select("#", ...) > 0 then
     return func(select(1,...)), map(func, select(2,...))
   end
end

-- 
Shmuel
 
CD: 3ms