This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Midnightas's profile picture

[solved] table.concat errors: string expected, got table

Started by Midnightas, 15 August 2016 - 06:57 PM
Midnightas #1
Posted 15 August 2016 - 08:57 PM
Very simple code, big-butt error:

table.concat(stack, ",")

That code should concatenate the table into a string with values seperated by a comma.
But then, I get this error:
bad argument: string expected, got table

I made sure stack was a table but that can't be the error since the only string argument it needs is actually a string.
Edited on 16 August 2016 - 05:30 PM
Lyqyd #2
Posted 15 August 2016 - 09:02 PM
What are the contents of the stack table?
Midnightas #3
Posted 15 August 2016 - 09:23 PM
stack is supposed to have another table at index 1, but I can't test it if table.concat does not work.
Midnightas #4
Posted 15 August 2016 - 09:33 PM
something to add: I even tried doing table.concat on the table that was supposed to be at index 1 before it was inserted.
It gave the same error.
KingofGamesYami #5
Posted 16 August 2016 - 12:23 AM
Ah, I see what is happening. table.concat works on tables of strings. Tables of other types, including tables don't work, and result in an error.


It is possible you want the textutils.serialize function, which takes a table and turns it (and it's contents) into a string.
Edited on 15 August 2016 - 10:23 PM
Midnightas #6
Posted 16 August 2016 - 06:35 AM
Thank you, will do!

1 question, any way to serialize without indentation and all prettifier stuff?
Emma #7
Posted 16 August 2016 - 06:49 AM
Thank you, will do!

1 question, any way to serialize without indentation and all prettifier stuff?

You could just remove all whitespace and newlines with a gsub, that would get rid of most of it (if not all)

local str = "   {  \n test		= \"example\",   \n  example  =	\"test\"   }"
local newString = string.gsub(str, "[%s\n]", "")

-- newString == {test="example",example="test"}
Edited on 16 August 2016 - 04:50 AM
Midnightas #8
Posted 16 August 2016 - 06:59 AM
what if the table contents have whitespace, though?
KingofGamesYami #9
Posted 16 August 2016 - 01:14 PM
Don't gsub the table, use textutils.serialize before the fancy stuff was added.
Emma #10
Posted 17 August 2016 - 01:14 AM
Don't gsub the table, use textutils.serialize before the fancy stuff was added.

I meant after it was serialized because textutils.serialize pretty prints the table.
As to the issue of whitespace in the values, don't gsub, just rewrite table.serialize and take out the parts that print whitespace.
KingofGamesYami #11
Posted 17 August 2016 - 01:32 AM
I meant after it was serialized because textutils.serialize pretty prints the table.
As to the issue of whitespace in the values, don't gsub, just rewrite table.serialize and take out the parts that print whitespace.

I linked a version of serialize which doesn't add whitespace in my previous post.
Emma #12
Posted 17 August 2016 - 05:31 PM
I meant after it was serialized because textutils.serialize pretty prints the table.
As to the issue of whitespace in the values, don't gsub, just rewrite table.serialize and take out the parts that print whitespace.

I linked a version of serialize which doesn't add whitespace in my previous post.

Ah, I assumed it was a link to the wiki xD