iterating over a table passed as an argument to a function in lua

405 views Asked by At

I am trying using the for _ in pairs() notation to iterate over a table within a function, but if I type anything, even gibberish like print('asdgfafs'), nested inside the for loop, it never gets printed. Code:

record = {bid1,bid2,bid3}
bid1 = {bidTime = 0.05,bidType = 'native'}
bid2 = {bidTime = 0.1,bidType = 'notNative'}
bid3 = {bidTime = 0.3,bidType = 'native'}

function getBids(rec,bidTimeStart,bidTimeFinish,bidType,numberOfBids)
    wantedBids = {}
    bidCount = 0

    for i,v in pairs(rec) do
        print('asdfasdfasdfa')
        print(i .. ' + ' .. v)
    end
end

getBids(record,0,1,'native',5)

Can anyone tell me why and suggest a workaround?

1

There are 1 answers

6
Etan Reisner On BEST ANSWER

You are creating the record table before creating the bid# tables.

So when you do record = {bid1, bid2, bid3} none of the bid# variables have been created yet and so they are all nil. So that line is effectively record = {nil, nil, nil} which, obviously, doesn't give the record table any values.

Invert those lines to put the record assignment after the bid# variable creation.