tip for making table.unpack(…) work

print(table.unpack(yourTable)) doesn’t really work like it’s supposed to, I think, but you can trick it into working right.

It’s supposed to list all the array elements in a table (unless it reaches a numbering gap in an index sequence), but in my experience it usually just prints out the first one.

But I’ve found that if you put empty quotes before the unpack command, and link them with a comma instead of .., it actually works correctly, so if instead of


…you write

     print(“”, table.unpack(yourTable))

…you’ll have a super-convenient way to do a quick inspection of the array part of any table!

have you an ecample of table unpack not working?

Might I turn that back on you and ask if you’ve had it work?

I use print statements for debugging all the time, as I assume most of us do, and I’ve run into this problem frequently enough that more than once I’ve written functions expressly for making printable strings out the contents of tables because I couldn’t get table.unpack(…) to work consistently.

If your experience is that it works flawlessly for you every time you’ve used it, I’d be very interested in knowing that.

So far table.unpack has worked for me. Probably won’t work on an indexed table, but I wouldn’t expect it to. It should work on any sequential table. Do you have one that it doesn’t work on.

yes, i just tested it and it worked as i expected.

a failing 4xample would help get it fixed, i’m think.

@dave1707 I’m confused by your terminology, but I think I know what you mean.

I think of a sequential table and an indexed table as the same thing—maybe I have the terms wrong myself, but the numbers that indicate the positions in a sequential table seem to usually be referred to as “indexes”, which leads me to connect that term to sequential tables.

I’m not sure what the technically proper terms are for the two different ways lua tables work, but I think the lua documents themselves consistently call the sequential part an array, and from the ubiquitous use of “k, v” in “for” statements I would speculate the other part of a lua table could be called the key-value part—though from @RonJeffries and various people on stack overflow I’ve taken to calling it the hash table. Maybe that’s even the right term, I don’t know, or maybe I’m getting the whole thing wrong start to finish!

At any rate it’s important to be using the same terms here or we’ll all get very confused, is it okay if we go with calling the sequential part an array and the key-value part a hash table?

…and that said, I’m referring to using table.unpack on arrays.

@UberGoober To me, an indexed table is a key, value table. You get the value in the table by using a key name. tab={a=“aaa”,b=“bbb”,c=“ccc”}. And for me a sequential table is a table that’s created using a sequence of values. tab={1,3,5,7,9}. The unpack works on the sequential table, but not on the keyed table.

Oh man this is super embarrassing, I hate this—now I can’t get it to happen!

Honest guys I’ve been struggling with this problem again and again, trying to figure out where my code was going wrong in one place or another, only to realize that I was getting inaccurate analysis because my table.unpack() statements weren’t working right…

I wish I knew how to make it happen now!

[@dave1707 it makes sense when you explain it, but normally one retrieves values in the table you’re calling “indexed” by using a “key,” and one retrieves values in the table you’re calling “sequential” by using an “index”… so you can see maybe how to other people that’s potentially a confusing way to refer to them?]

@UberGoober Normally I don’t index a sequential table because I don’t know what’s at a specific index. I’ll read the table sequentially and use what I read. For instance, I’ll have table of sprites that I display at some x,y screen position. I’ll update their x,y position as they move around the screen.