Help please:Rewriting tables? Syntax error?

Hey guys, thanks in advance for any help you offer

So I’ve been working with tables recently, and they’re very useful and simple to understand for the most part, however I’m not sure how to search them

such as if i have a table

local table = (

{variable1 = …, variable2 = …},

)

is there a command to find a row where lets say, variable1 = abc? Meaning i set the command to search for this string for variable1 and it returns for which row this is true.

another question i have is say i have the table

local table = (

{variable1 = desk, variable2 = chair}

)

Could i rewrite row1 without deleting/rewriting the entire table? i.e. change variable1 to equal pencil or something? I know i can insert new rows but is it possible to edit a pre existing row?

on a somewhat unrelated note i have an error i cant get rid of and im not sure even sure where to start or what to post it under

it appeared right after i fixed another error where for some reason it didnt like my (button widget).x=(number) command to set the coordinates of on of my buttons

thats the original error if it helps

along with the lines of code it references

(lines 156/157)

cardOneBtn.x = 10 cardOneBtn.y = 10

 button/widget its referring to

 local cardOneBtn = widget.newButton { width = 50, height = 100, defaultFile = "(cardIndex[one].pic)", overFile = "(cardIndex[one].pic)", onEvent = addCardOne }

The above widget is contained within a local function XXXX(event)

to ‘fix’ my first error i took it out of that function and put it out on its own.

thanks in advance for any help

It sounds like you’re asking multiple questions in one thread, which is going to make it tough to focus on one answer.

For the errors, this feels like a scope issue, but we can’t see enough of your code to tell. When you declare something local it can only be seen in the block of code where you declared it and and nested/children blocks. So if:

local cardOneBtn = widget.newButton

is inside a function, only that function can see it. If you’re trying to reference it from another function, you will get this error. The solution is to do:

local cardOneBtn

at the top of your module outside of any functions. Then when you create the button, leave the “local” off a that point.

As for your first table question, you can use a for loop using pairs to loop over the table and look for the data you want:

for k, v in pairs(yourTable) do

     if k == “variable1” and value == “chair” then

          – do something with the data

     end

end

For the 2nd table question, yes:

yourTable.variable1 = “some new value”.

Please do not use the word “table” as a name for your table variables. “table” is a reserved word (sort of), its the API’s for working with tables. If you use a variable named “table” you blow away these other API’s you might want to use.

Rob

Getting rid of the function around the widget didnt fix the issue, however putting the (button).x = (co ordinate) inside the function did fix the issue to some degree, the error has disappeared but the button isnt showing.

if anyone wants to look through it here is the code

local composer = require( "composer" ) local scene = composer.newScene() local cardIndex = require( "cardIndex") local deck = require( "deck") local widget = require ("widget") local one = 1 local two =2 local three = 3 local four = 4 local five = 5 function scene:create( event ) local sceneGroup = self.view --Put background in here end function scene:show( event ) local sceneGroup = self.view if ( event.phase == "did" ) then local function goMenu( event ) if ("ended" == event.phase) then composer.gotoScene( "Menu" ) end end local menuBtn = widget.newButton { width = 100, height = 50, defaultFile = "RECO.png", overFile = "RECO.png", label = "menu", onEvent = goMenu } menuBtn.x = 160 menuBtn.y = 240 sceneGroup:insert(menuBtn) local function goRight( event ) if ("ended" == event.phase) then one = one + 5 two = two + 5 three = three + 5 four = four + 5 five = five + 5 onEvent = refresh end end local function goLeft( event ) if ("ended" == event.phase) then one = one - 5 two = two - 5 three = three - 5 four = four - 5 five = five - 5 onEvent = refresh end end local function refresh( event ) local cardOneBtn = widget.newButton { width = 50, height = 100, defaultFile = "(cardIndex[one].pic)", overFile = "(cardIndex[one].pic)", onEvent = addCardOne } local cardTwoBtn = widget.newButton { width = 50, height = 100, defaultFile = "(cardIndex[two].pic)", overFile = "(cardIndex[two].pic)", onEvent = addCardTwo } local cardThreeBtn = widget.newButton { width = 50, height = 100, defaultFile = "(cardIndex[three].pic)", overFile = "(cardIndex[three].pic)", onEvent = addCardThree } local cardFourBtn = widget.newButton { width = 50, height = 100, defaultFile = "(cardIndex[four].pic)", overFile = "(cardIndex[four].pic)", onEvent = addCardFour } local cardFiveBtn = widget.newButton { width = 50, height = 100, defaultFile = "(cardIndex[five].pic)", overFile = "(cardIndex[five].pic)", onEvent = addCardFive } cardOneBtn.x = 10 cardOneBtn.y = 10 sceneGroup:insert( cardOneBtn ) cardTwoBtn.x = 100 cardTwoBtn.y = 10 sceneGroup:insert( cardTwoBtn ) cardThreeBtn.x = 200 cardThreeBtn.y = 10 sceneGroup:insert( cardThreeBtn ) cardFourBtn.x = 10 cardFourBtn.y = 300 sceneGroup:insert( cardFourBtn ) cardFiveBtn.x =10 cardFiveBtn.y = 400 sceneGroup:insert( cardFiveBtn ) end local function addCardOne( event ) if event.phase == "ended" then deck.insert( {(cardIndex[one])} ) end end local function addCardTwo( event ) if event.phase == "ended" then deck.insert( {(cardIndex[two])} ) end end local function addCardThree( event ) if event.phase == "ended" then deck.insert( {(cardIndex[three])} ) end end local function addCardFour( event ) if event.phase == "ended" then deck.insert( {(cardIndex[four])} ) end end local function addCardFive( event ) if event.phase == "ended" then deck.insert( {(cardIndex[five])} ) end end local leftBtn = widget.newButton { width = 50, height = 50, defaultFile = "RECO.png", overFile = "RECO.png", label = "\<", onEvent = goLeft } local rightBtn = widget.newButton { width = 50, height = 50, defaultFile = "RECO.png", overFile = "RECO.png", label = "\>", onEvent = goRight } rightBtn.x = 100 rightBtn.y = 100 leftBtn.x = 100 leftBtn.y = 200 sceneGroup:insert( leftBtn ) sceneGroup:insert( rightBtn ) local function hideLeft( event ) leftBtn.isVisible = false end local function showLeft( event ) leftBtn.isVisible = true end local function hideRight( event ) rightBtn.isVisible = false end local function showRight( event ) rightBtn.isVisible = true end scene:addEventListener( "one = 1", hideLeft ) scene:addEventListener( "one \> 1", showLeft ) scene:addEventListener( "(cardIndex[five].exist) = 0", hideRight) scene:addEventListener( "(cardIndex[five].exist) \> 0", showRight) end end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) return scene

The scene isnt returning an error or crashing, the return to menu button still works and the left/right buttons show up just fine however the card buttons/images do not show up or work.

heres the table being referenced 

local cardIndex = { { cat = "a", subcat = "a", cardType = "a", name = "a", def = "a", atk = "a", effect = "a", pic = "jack1.png" , exist = 1 }, { cat = "a", subcat = "a", cardType = "a", name = "a", def = "a", atk = "a", effect = "a", pic = "jack2.png" , exist = 1 }, { cat = "a", subcat = "a", cardType = "a", name = "a", def = "a", atk = "a", effect = "a", pic = "jack3.png" , exist = 1 }, { cat = "a", subcat = "a", cardType = "a", name = "a", def = "a", atk = "a", effect = "a", pic = "jack4.png" , exist = 1 }, { cat = "a", subcat = "a", cardType = "a", name = "a", def = "a", atk = "a", effect = "a", pic = "jack5.png" , exist = 1 }, { cat = "a", subcat = "a", cardType = "a", name = "a", def = "a", atk = "a", effect = "a", pic = "jack6.png" , exist = 1 }, { cat = "a", subcat = "a", cardType = "a", name = "a", def = "a", atk = "a", effect = "a", pic = "jack7.png" , exist = 1 }, { cat = "a", subcat = "a", cardType = "a", name = "a", def = "a", atk = "a", effect = "a", pic = "a", exist = 0 }, { cat = "a", subcat = "a", cardType = "a", name = "a", def = "a", atk = "a", effect = "a", pic = "a", exist = 0 }, { cat = "a", subcat = "a", cardType = "a", name = "a", def = "a", atk = "a", effect = "a", pic = "a", exist = 0 }, { cat = "a", subcat = "a", cardType = "a", name = "a", def = "a", atk = "a", effect = "a", pic = "a", exist = 0 } } return cardIndex

Also, something that came to mind, the code i’ve written refers to and adds to a table called ‘deck’ which is in another lua folder, does this overwrite/permanently change that table or do any changes get lost when the app is closed?

anyways, thanks so much for the help

Just wondering, if i have several rows each with a ‘variable1’ like in the above table, does doing this rewrite every instance of ‘variable1’ as this new value? i was wondering if i could somehow just rewrite a specific row

You probably should read a couple of tutorials before proceeding:

This will help explain how to organize your code. You have functions inside of functions and all this is going to do is create scope issues for you.

https://coronalabs.com/blog/2015/06/16/tutorial-scope-for-beginners/

And to help you understand how your code is laid out, this tutorial can help you write code that is readable for both you and those who you ask to help.

https://coronalabs.com/blog/2015/06/09/tutorial-the-value-of-well-formatted-code/

Rob

It sounds like you’re asking multiple questions in one thread, which is going to make it tough to focus on one answer.

For the errors, this feels like a scope issue, but we can’t see enough of your code to tell. When you declare something local it can only be seen in the block of code where you declared it and and nested/children blocks. So if:

local cardOneBtn = widget.newButton

is inside a function, only that function can see it. If you’re trying to reference it from another function, you will get this error. The solution is to do:

local cardOneBtn

at the top of your module outside of any functions. Then when you create the button, leave the “local” off a that point.

As for your first table question, you can use a for loop using pairs to loop over the table and look for the data you want:

for k, v in pairs(yourTable) do

     if k == “variable1” and value == “chair” then

          – do something with the data

     end

end

For the 2nd table question, yes:

yourTable.variable1 = “some new value”.

Please do not use the word “table” as a name for your table variables. “table” is a reserved word (sort of), its the API’s for working with tables. If you use a variable named “table” you blow away these other API’s you might want to use.

Rob

Getting rid of the function around the widget didnt fix the issue, however putting the (button).x = (co ordinate) inside the function did fix the issue to some degree, the error has disappeared but the button isnt showing.

if anyone wants to look through it here is the code

local composer = require( "composer" ) local scene = composer.newScene() local cardIndex = require( "cardIndex") local deck = require( "deck") local widget = require ("widget") local one = 1 local two =2 local three = 3 local four = 4 local five = 5 function scene:create( event ) local sceneGroup = self.view --Put background in here end function scene:show( event ) local sceneGroup = self.view if ( event.phase == "did" ) then local function goMenu( event ) if ("ended" == event.phase) then composer.gotoScene( "Menu" ) end end local menuBtn = widget.newButton { width = 100, height = 50, defaultFile = "RECO.png", overFile = "RECO.png", label = "menu", onEvent = goMenu } menuBtn.x = 160 menuBtn.y = 240 sceneGroup:insert(menuBtn) local function goRight( event ) if ("ended" == event.phase) then one = one + 5 two = two + 5 three = three + 5 four = four + 5 five = five + 5 onEvent = refresh end end local function goLeft( event ) if ("ended" == event.phase) then one = one - 5 two = two - 5 three = three - 5 four = four - 5 five = five - 5 onEvent = refresh end end local function refresh( event ) local cardOneBtn = widget.newButton { width = 50, height = 100, defaultFile = "(cardIndex[one].pic)", overFile = "(cardIndex[one].pic)", onEvent = addCardOne } local cardTwoBtn = widget.newButton { width = 50, height = 100, defaultFile = "(cardIndex[two].pic)", overFile = "(cardIndex[two].pic)", onEvent = addCardTwo } local cardThreeBtn = widget.newButton { width = 50, height = 100, defaultFile = "(cardIndex[three].pic)", overFile = "(cardIndex[three].pic)", onEvent = addCardThree } local cardFourBtn = widget.newButton { width = 50, height = 100, defaultFile = "(cardIndex[four].pic)", overFile = "(cardIndex[four].pic)", onEvent = addCardFour } local cardFiveBtn = widget.newButton { width = 50, height = 100, defaultFile = "(cardIndex[five].pic)", overFile = "(cardIndex[five].pic)", onEvent = addCardFive } cardOneBtn.x = 10 cardOneBtn.y = 10 sceneGroup:insert( cardOneBtn ) cardTwoBtn.x = 100 cardTwoBtn.y = 10 sceneGroup:insert( cardTwoBtn ) cardThreeBtn.x = 200 cardThreeBtn.y = 10 sceneGroup:insert( cardThreeBtn ) cardFourBtn.x = 10 cardFourBtn.y = 300 sceneGroup:insert( cardFourBtn ) cardFiveBtn.x =10 cardFiveBtn.y = 400 sceneGroup:insert( cardFiveBtn ) end local function addCardOne( event ) if event.phase == "ended" then deck.insert( {(cardIndex[one])} ) end end local function addCardTwo( event ) if event.phase == "ended" then deck.insert( {(cardIndex[two])} ) end end local function addCardThree( event ) if event.phase == "ended" then deck.insert( {(cardIndex[three])} ) end end local function addCardFour( event ) if event.phase == "ended" then deck.insert( {(cardIndex[four])} ) end end local function addCardFive( event ) if event.phase == "ended" then deck.insert( {(cardIndex[five])} ) end end local leftBtn = widget.newButton { width = 50, height = 50, defaultFile = "RECO.png", overFile = "RECO.png", label = "\<", onEvent = goLeft } local rightBtn = widget.newButton { width = 50, height = 50, defaultFile = "RECO.png", overFile = "RECO.png", label = "\>", onEvent = goRight } rightBtn.x = 100 rightBtn.y = 100 leftBtn.x = 100 leftBtn.y = 200 sceneGroup:insert( leftBtn ) sceneGroup:insert( rightBtn ) local function hideLeft( event ) leftBtn.isVisible = false end local function showLeft( event ) leftBtn.isVisible = true end local function hideRight( event ) rightBtn.isVisible = false end local function showRight( event ) rightBtn.isVisible = true end scene:addEventListener( "one = 1", hideLeft ) scene:addEventListener( "one \> 1", showLeft ) scene:addEventListener( "(cardIndex[five].exist) = 0", hideRight) scene:addEventListener( "(cardIndex[five].exist) \> 0", showRight) end end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) return scene

The scene isnt returning an error or crashing, the return to menu button still works and the left/right buttons show up just fine however the card buttons/images do not show up or work.

heres the table being referenced 

local cardIndex = { { cat = "a", subcat = "a", cardType = "a", name = "a", def = "a", atk = "a", effect = "a", pic = "jack1.png" , exist = 1 }, { cat = "a", subcat = "a", cardType = "a", name = "a", def = "a", atk = "a", effect = "a", pic = "jack2.png" , exist = 1 }, { cat = "a", subcat = "a", cardType = "a", name = "a", def = "a", atk = "a", effect = "a", pic = "jack3.png" , exist = 1 }, { cat = "a", subcat = "a", cardType = "a", name = "a", def = "a", atk = "a", effect = "a", pic = "jack4.png" , exist = 1 }, { cat = "a", subcat = "a", cardType = "a", name = "a", def = "a", atk = "a", effect = "a", pic = "jack5.png" , exist = 1 }, { cat = "a", subcat = "a", cardType = "a", name = "a", def = "a", atk = "a", effect = "a", pic = "jack6.png" , exist = 1 }, { cat = "a", subcat = "a", cardType = "a", name = "a", def = "a", atk = "a", effect = "a", pic = "jack7.png" , exist = 1 }, { cat = "a", subcat = "a", cardType = "a", name = "a", def = "a", atk = "a", effect = "a", pic = "a", exist = 0 }, { cat = "a", subcat = "a", cardType = "a", name = "a", def = "a", atk = "a", effect = "a", pic = "a", exist = 0 }, { cat = "a", subcat = "a", cardType = "a", name = "a", def = "a", atk = "a", effect = "a", pic = "a", exist = 0 }, { cat = "a", subcat = "a", cardType = "a", name = "a", def = "a", atk = "a", effect = "a", pic = "a", exist = 0 } } return cardIndex

Also, something that came to mind, the code i’ve written refers to and adds to a table called ‘deck’ which is in another lua folder, does this overwrite/permanently change that table or do any changes get lost when the app is closed?

anyways, thanks so much for the help

Just wondering, if i have several rows each with a ‘variable1’ like in the above table, does doing this rewrite every instance of ‘variable1’ as this new value? i was wondering if i could somehow just rewrite a specific row

You probably should read a couple of tutorials before proceeding:

This will help explain how to organize your code. You have functions inside of functions and all this is going to do is create scope issues for you.

https://coronalabs.com/blog/2015/06/16/tutorial-scope-for-beginners/

And to help you understand how your code is laid out, this tutorial can help you write code that is readable for both you and those who you ask to help.

https://coronalabs.com/blog/2015/06/09/tutorial-the-value-of-well-formatted-code/

Rob