Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to print single value of a table in lua

I can’t figure out how to print a single value in tab2.

I’m trying to get the same result of when I print tab1.

I don’t care about the order I just need to print a single pair(get the the key and the value), or pop it from the tab2 (with something like table.remove(tab1,1))

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

tab1= {{x=1},{y=2}}
tab2= {x=3, y=4}

for k,v in pairs(tab1[1]) do
    print(k,v)
end

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

I’m having difficult with tables, I started study three days ago so I’m a principiant.

Thank you

>Solution :

To print a single value from tab2 you do

print(tab2["x"]) or print(tab2["y"]).

Or short:

print(tab2.x) or print(tab2.y)

Thank you for your fast response. I need a way to do it when I don’t
know the key.

In order to get a single value from a table without providing a key you can use next

print(next(tab2))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading