1

I have a table "weather". I insert weather conditions for a particular day. I can't seem to write a function that prints the contents of "weather" (see below for things I've tried.

day = "Friday"
conditions = {"Sunny", "85", "windy"}
weather = {{}}    --nested table


for k, v in pairs(conditions) do     
    weather[day] = {[k]=v}  
end

I've tried two things to print the weather table and neither work.

for k, v in pairs(weather) do 
print(k, v)
end

---- Output ---
1       table: 0x2542ae0
Friday  table: 0x25431a0

This doesn't work either, but I thought it would

for k, v in pairs(weather) do 
    for l, w in pairs(v) do
    print(l, w)
    end
end

----Output----
3   windy

1 Answer 1

1

You are overwriting weather[day] in the first loop and so only the last value remains.

I think you want simply this, instead of that loop:

weather[day] = conditions  
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.