bad argument #1 to 'sub' (string expected, got table)

I have a class named deck.lua

local deck={} local deck\_mt={\_\_index=deck} function deck.new() -- data -- deck.card\_list={} -- logic -- ............... table.insert(deck.card\_list, ....) -- populate deck.card\_list return setmetatable(deck\_new, deck\_mt) end

I am accessing deck.card_list in main.lua , but i get an error on: cardNumber and cardSuit

“bad argument #1 to ‘sub’ (string expected, got table)”

local deck=deck\_t.new() local random\_number=math.random(#deck.card\_list) local cardNumber = tonumber(string.sub(deck.card\_list[random\_number],2,3)) local cardSuit = string.sub(deck.card\_list[random\_number],1,1) table.remove(deck.card\_list,random\_number)

Examine what your code is doing, step by step… 

local deck=deck\_t.new() print("1 - deck", deck ) local random\_number=math.random(#deck.card\_list) print("2 - random\_number", random\_number ) print("3 - deck.card\_list", deck.card\_list ) print("4 - deck.card\_list[random\_number]", deck.card\_list[random\_number] ) print("5 - string.sub(deck.card\_list[random\_number],2,3)", string.sub(deck.card\_list[random\_number],2,3) ) local cardNumber = tonumber(string.sub(deck.card\_list[random\_number],2,3)) print("6 - deck.card\_list", deck.card\_list ) print("7 - deck.card\_list[random\_number]", deck.card\_list[random\_number] ) print("8 - string.sub(deck.card\_list[random\_number],1,1)", string.sub(deck.card\_list[random\_number],1,1) ) local cardSuit = string.sub(deck.card\_list[random\_number],1,1) print("9 - cardSuit", cardSuit )

then debug each failure as you come to it.

This is what you’re asking us to do for you.  But you can achieve this on your own.

I hope this doesn’t seem like a ‘mean’ response.  The thing is, there is no mystery here. You just need to learn to break down the problem into smaller chunks and figure out the issue.

I promise if you do this kind of detailed analysis on your own, you’ll learn more and get better much faster than having the issue pointed out by a helpful reader.

Finally, if you do break down the steps and print out the results of each part, be sure what prints out is what you expect for each stage.

I can tell you now, at least one step along the way is producing a result that cannot be fed into the next step.  

“Why,” is the question.  “Why are you getting a table instead of a string?” Track the code as it executes and it should become clear.

Thank you @romainggamer,

No i don’t take it mean at all. I understand that printing values in corona it’s a very helpful way to debug the code.

I thought problem might stand on syntax string.sub, because i get the error @this line:

print(“5  - string.sub(deck.card_list[random_number],2,3)”, string.sub(deck.card_list[random_number],2,3) )

which leads on not showing the rest of lines on terminal.

Regards

Please post the results of debug lines 1…4 in a code block, and I’ll comment more on what is happening.

Also, try this:

for k,v in pairs( deck.card\_list ) print(k,v) end

Share a few lines of the output with us (in addition to the results I asked for above).

My first guess here is: The … in this code is a table (or tables).

table.insert(deck.card\_list, ... ) -- Are you inserting tables? Print out the things you're inserting before doing the insert to check this.

on deck class

function deck.new() -- data -- deck.card\_list={} -- logic -- for suit=1, 4 do for number=1, 13 do table.insert(deck.card\_list, card\_t.new(main,suit\_list[suit],number,0,0)) print("1.deck suite", suit\_list[suit]) //OK print("2.deck number", number) //OK end end return setmetatable(deck, deck\_mt) end

on main
 

local deck=deck\_t.new() print("1 - deck", deck ) //1 - deck table: 0x60800126adc0 local random\_number=math.random(#deck.card\_list) print("2 - random\_number", random\_number ) //2 - random\_number 5 print("3 - deck.card\_list", deck.card\_list ) //3 - deck.card\_list table: 0x600001078dc0 print("4 - deck.card\_list[random\_number]", deck.card\_list[random\_number] ) //4 - deck.card\_list[random\_number] table: 0x608001678180 local deckstring=tostring(deck.card\_list[random\_number]) print("4'' - deckstring", deckstring) //4'' - deckstring table: 0x608001678180 print("5 - string.sub(deck.card\_list[random\_number],2,3)", string.sub(deckstring,2,3) ) //5 - string.sub(deck.card\_list[random\_number],2,3) ab local cardNumber = tonumber(string.sub(deck.card\_list[random\_number],2,3)) print("6 - deck.card\_list", deck.card\_list ) print("7 - deck.card\_list[random\_number]", deck.card\_list[random\_number] ) print("8 - string.sub(deck.card\_list[random\_number],1,1)", string.sub(deck.card\_list[random\_number],1,1) ) local cardSuit = string.sub(deck.card\_list[random\_number],1,1) print("9 - cardSuit", cardSuit )

//problem stands on number 5 -> prints “ab”

print("4 - deck.card\_list[random\_number]", deck.card\_list[random\_number] ) 

producing this:

4 - deck.card\_list[random\_number] table: 0x608001678180

tells me, somewhere in the past, you’ve inserted tables into ‘deck.card_list’, not numbers.

Show the actual code where you are actually initializing ‘deck.card_list’.

Yes you are right i am adding tables into deck.card_list instead of numbers:

deck.card_list={}

table.insert(deck.card_list, card_t.new(main,suit_list[suit],number,0,0))

So, have you fixed it and gotten it working?  I’ll assume yes.

For future readers, the lesson to take away is, “question your assumptions”.  

In this case, the specious assumption was that the data in ‘deck.card_list’ was good and something was wrong with string.sub(), but in actuality, the data was bad (i.e. Tables and not numbers.).

-Ed

Meanwhile, blablu1212, keep it up and good luck with your project.

Examine what your code is doing, step by step… 

local deck=deck\_t.new() print("1 - deck", deck ) local random\_number=math.random(#deck.card\_list) print("2 - random\_number", random\_number ) print("3 - deck.card\_list", deck.card\_list ) print("4 - deck.card\_list[random\_number]", deck.card\_list[random\_number] ) print("5 - string.sub(deck.card\_list[random\_number],2,3)", string.sub(deck.card\_list[random\_number],2,3) ) local cardNumber = tonumber(string.sub(deck.card\_list[random\_number],2,3)) print("6 - deck.card\_list", deck.card\_list ) print("7 - deck.card\_list[random\_number]", deck.card\_list[random\_number] ) print("8 - string.sub(deck.card\_list[random\_number],1,1)", string.sub(deck.card\_list[random\_number],1,1) ) local cardSuit = string.sub(deck.card\_list[random\_number],1,1) print("9 - cardSuit", cardSuit )

then debug each failure as you come to it.

This is what you’re asking us to do for you.  But you can achieve this on your own.

I hope this doesn’t seem like a ‘mean’ response.  The thing is, there is no mystery here. You just need to learn to break down the problem into smaller chunks and figure out the issue.

I promise if you do this kind of detailed analysis on your own, you’ll learn more and get better much faster than having the issue pointed out by a helpful reader.

Finally, if you do break down the steps and print out the results of each part, be sure what prints out is what you expect for each stage.

I can tell you now, at least one step along the way is producing a result that cannot be fed into the next step.  

“Why,” is the question.  “Why are you getting a table instead of a string?” Track the code as it executes and it should become clear.

Thank you @romainggamer,

No i don’t take it mean at all. I understand that printing values in corona it’s a very helpful way to debug the code.

I thought problem might stand on syntax string.sub, because i get the error @this line:

print(“5  - string.sub(deck.card_list[random_number],2,3)”, string.sub(deck.card_list[random_number],2,3) )

which leads on not showing the rest of lines on terminal.

Regards

Please post the results of debug lines 1…4 in a code block, and I’ll comment more on what is happening.

Also, try this:

for k,v in pairs( deck.card\_list ) print(k,v) end

Share a few lines of the output with us (in addition to the results I asked for above).

My first guess here is: The … in this code is a table (or tables).

table.insert(deck.card\_list, ... ) -- Are you inserting tables? Print out the things you're inserting before doing the insert to check this.

on deck class

function deck.new() -- data -- deck.card\_list={} -- logic -- for suit=1, 4 do for number=1, 13 do table.insert(deck.card\_list, card\_t.new(main,suit\_list[suit],number,0,0)) print("1.deck suite", suit\_list[suit]) //OK print("2.deck number", number) //OK end end return setmetatable(deck, deck\_mt) end

on main
 

local deck=deck\_t.new() print("1 - deck", deck ) //1 - deck table: 0x60800126adc0 local random\_number=math.random(#deck.card\_list) print("2 - random\_number", random\_number ) //2 - random\_number 5 print("3 - deck.card\_list", deck.card\_list ) //3 - deck.card\_list table: 0x600001078dc0 print("4 - deck.card\_list[random\_number]", deck.card\_list[random\_number] ) //4 - deck.card\_list[random\_number] table: 0x608001678180 local deckstring=tostring(deck.card\_list[random\_number]) print("4'' - deckstring", deckstring) //4'' - deckstring table: 0x608001678180 print("5 - string.sub(deck.card\_list[random\_number],2,3)", string.sub(deckstring,2,3) ) //5 - string.sub(deck.card\_list[random\_number],2,3) ab local cardNumber = tonumber(string.sub(deck.card\_list[random\_number],2,3)) print("6 - deck.card\_list", deck.card\_list ) print("7 - deck.card\_list[random\_number]", deck.card\_list[random\_number] ) print("8 - string.sub(deck.card\_list[random\_number],1,1)", string.sub(deck.card\_list[random\_number],1,1) ) local cardSuit = string.sub(deck.card\_list[random\_number],1,1) print("9 - cardSuit", cardSuit )

//problem stands on number 5 -> prints “ab”

print("4 - deck.card\_list[random\_number]", deck.card\_list[random\_number] ) 

producing this:

4 - deck.card\_list[random\_number] table: 0x608001678180

tells me, somewhere in the past, you’ve inserted tables into ‘deck.card_list’, not numbers.

Show the actual code where you are actually initializing ‘deck.card_list’.