Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

bad argument #1 to 'sub' (string expected, got table)
Started by blablu1212 Apr 19 2015 01:26 PM

- - - - -
11 replies to this topic
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

blablu1212

[GLOBAL: userInfoPane.html]
blablu1212
  • Contributor

  • 122 posts
  • Corona SDK

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)

 

 



[TOPIC: post.html]
#2

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,587 posts
  • Corona SDK

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.


Edited by roaminggamer, 19 April 2015 - 03:16 PM.


[TOPIC: post.html]
#3

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,587 posts
  • Corona SDK

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.



[TOPIC: post.html]
#4

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,587 posts
  • Corona SDK

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.


Edited by roaminggamer, 19 April 2015 - 03:21 PM.


[TOPIC: post.html]
#5

blablu1212

[GLOBAL: userInfoPane.html]
blablu1212
  • Contributor

  • 122 posts
  • Corona SDK

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



[TOPIC: post.html]
#6

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,587 posts
  • Corona SDK

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



[TOPIC: post.html]
#7

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,587 posts
  • Corona SDK

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.


[TOPIC: post.html]
#8

blablu1212

[GLOBAL: userInfoPane.html]
blablu1212
  • Contributor

  • 122 posts
  • Corona SDK

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"



[TOPIC: post.html]
#9

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,587 posts
  • Corona SDK

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'.



[TOPIC: post.html]
#10

blablu1212

[GLOBAL: userInfoPane.html]
blablu1212
  • Contributor

  • 122 posts
  • Corona SDK

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))



[TOPIC: post.html]
#11

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,587 posts
  • Corona SDK

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



[TOPIC: post.html]
#12

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,587 posts
  • Corona SDK

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




[topic_controls]
[/topic_controls]