Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create table from two input tables .output table's key will be from first input and values will be from second input table

Tags:

lua

lua-table

I am having a table in which I am passing names:

names = {'Sachin', 'Ponting', 'Dhoni'}

and in other table I am passing country names:

country = {"India", "Australia", "India"}

I want output table like:

out_table = {Sachin="India", Ponting="Australia", Dhoni="India"}

like image 608
Prashant Gaur Avatar asked Oct 02 '22 10:10

Prashant Gaur


1 Answers

Here's a straight-forward attempt:

names = {'Sachin', 'Ponting', 'Dhoni'}
countries = {"India", "Australia", "India"}

function table_map(names, countries)
    local out = {}
    for i, each in ipairs(names) do
        out[each] = countries[i]
    end
    return out
end

out_table = table_map(names, countries)

Live repl demo.

like image 193
greatwolf Avatar answered Oct 24 '22 10:10

greatwolf