SlickLeo
SlickLeo

Reputation: 33

Searching through tables, I'm a bit stuck

I need a bit of help with tables.

function t (data)
    local t = {}
    for _, l in ipairs(data) do t[l] = true end
    return t
end

local data = t {['b2'] = '-9 on block, KND on hit, 16f startup.'};

io.write('What do you want to know?', '\n');
re = io.read();
if data[re] then
    print('Yo');
end

What I'm trying to do, is if I put in something that's in the data table (in this case I put in b2 when it tells me what I want to know), it will print '-9 on block, KND on hit, 16f startup'.

Upvotes: 2

Views: 45

Answers (1)

Yu Hao
Yu Hao

Reputation: 122383

I have no idea why you need the function t. The table itself is a associative array data structure. A cleaner version of code is like this:

local data = {b2 = '-9 on block, KND on hit, 16f startup.'}

print('What do you want to know?')
re = io.read()
if data[re] then
    print(data[re])
end

Note the use of print instead of io.write and I also removed all the useless semicolons.

Upvotes: 3

Related Questions