Jump to content




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


  • You cannot reply to this topic
11 replies to this topic

#1 Midnightas

  • Members
  • 44 posts

Posted 15 August 2016 - 07: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 by Midnightas, 16 August 2016 - 06:30 PM.


#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 15 August 2016 - 08:02 PM

What are the contents of the stack table?

#3 Midnightas

  • Members
  • 44 posts

Posted 15 August 2016 - 08:23 PM

stack is supposed to have another table at index 1, but I can't test it if table.concat does not work.

#4 Midnightas

  • Members
  • 44 posts

Posted 15 August 2016 - 08: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.

#5 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 15 August 2016 - 11:23 PM

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 by KingofGamesYami, 15 August 2016 - 11:23 PM.


#6 Midnightas

  • Members
  • 44 posts

Posted 16 August 2016 - 05:35 AM

Thank you, will do!

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

#7 Emma

  • Members
  • 216 posts
  • Locationtmpim

Posted 16 August 2016 - 05:49 AM

View PostMidnightas, on 16 August 2016 - 05:35 AM, said:

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 by Incinirate, 16 August 2016 - 05:50 AM.


#8 Midnightas

  • Members
  • 44 posts

Posted 16 August 2016 - 05:59 AM

what if the table contents have whitespace, though?

#9 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 16 August 2016 - 12:14 PM

Don't gsub the table, use textutils.serialize before the fancy stuff was added.

#10 Emma

  • Members
  • 216 posts
  • Locationtmpim

Posted 17 August 2016 - 12:14 AM

View PostKingofGamesYami, on 16 August 2016 - 12:14 PM, said:

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.

#11 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 17 August 2016 - 12:32 AM

View PostIncinirate, on 17 August 2016 - 12:14 AM, said:


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.

#12 Emma

  • Members
  • 216 posts
  • Locationtmpim

Posted 17 August 2016 - 04:31 PM

View PostKingofGamesYami, on 17 August 2016 - 12:32 AM, said:

View PostIncinirate, on 17 August 2016 - 12:14 AM, said:

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





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users