good syntax to replace anything

Hi,

i’m coming from bash and in bash you can do

grid[\*][\*] 

but not in lua

What’is the good syntax to do that ?

Thanks

Lua doesn’t have regular expressions (as operators on tables).

You have to iterate:

for i = 1, #grid do for j = 1 #grid[i] do grid[i][j] = ... end end -- Or, for non-integer indexed tables. for k,v in pairs(grid) do for l,m in pairs(v) do m = ... end end

hi Roaminggamer and thanks but it will be easier with a concrete example and this is the result of another one of my post for what you helped about me lib grid.lua. i have tried like this but it doesn’t work…i would like ton animate all my grid.

local grid = require("grid") local gridGame = grid.newGrid(9, 15, screenWidth - margins) function gridtransition()  for k,v in pairs(gridGame) do    for l,m in pairs(v) do       m = 15    end end end   transition.to( gridtransition, {tag="squareTransition", yScale=5, xScale=5, time=3000 } )

Hi.  The last line of your code isn’t making any sense to me.  The first argument needs to be a display object, not a function.

local obj = display.newCircle( ... ) transition.to( obj, { x = 100, time = 1000} ) 

Also, I take it that gridGame is a table of tables?  By setting ‘m = 15’, you don’t do anything programatically except assign 15 to the temporary variable ‘m’.  Lua does not have ‘pointers’, only references.

for k,v in pairs(gridGame) do for l,m in pairs(v) do -- At this point, m contains a reference to a table m = 15 -- At this point m contains 15, the table is unchanged -- m is a variable, not a pointer end end

Maybe this is what you want?

-- This function assumes that your grid has this stucture -- --[[aGrid = { { objA, objB, objC, ..., }, ... { obj?, obj?, obj?, ..., }, } local testObj = aGrid[1][1] -- testObj is assigned objA local testObj = aGrid[1][3] -- testObj is assigned objC --]] function gridtransition( aGrid ) for k,v in pairs( aGrid ) do for l,m in pairs(v) do transition.to( m , { ... your parameters here } ) end end end

On a final note, I find multi-dimensional tables to be a terrible way to track objects.  Better, figure out their row and column using their positions instead.  However, as I’ve said in other posts, “to each his or her own”.

Hi again.  It occurred to me that you are storing values in your array.  Then you can change values like this:

-- This example assumes that your grid has this stucture -- --[[aGrid = { { 10, "dog", 20, ..., }, { 15, "god", 3.1415, ..., }, ... } print( aGrid[1][2], aGrid[2][3] ) -- prints: dog 3.1415 --]] for k,v in pairs( aGrid ) do for l,m in pairs(v) do -- at this point m contains a value retrieved from v[l] -- k and l are table 'keys' -- v and m are values at a position in the tables 'aGrid' and v respectively v[l] = 12 -- Now, the value at v[l] has been replaced with 12, while the temporary value in m is the same. end end end

thanks,

I must admit that at first I did not understand at all  :frowning: but reading and rereading I finally understand, thank you for the explanation :slight_smile:
I like this little phrase:
nothing is difficult to understand, you just have to understand.

Lua doesn’t have regular expressions (as operators on tables).

You have to iterate:

for i = 1, #grid do for j = 1 #grid[i] do grid[i][j] = ... end end -- Or, for non-integer indexed tables. for k,v in pairs(grid) do for l,m in pairs(v) do m = ... end end

hi Roaminggamer and thanks but it will be easier with a concrete example and this is the result of another one of my post for what you helped about me lib grid.lua. i have tried like this but it doesn’t work…i would like ton animate all my grid.

local grid = require("grid") local gridGame = grid.newGrid(9, 15, screenWidth - margins) function gridtransition()  for k,v in pairs(gridGame) do    for l,m in pairs(v) do       m = 15    end end end   transition.to( gridtransition, {tag="squareTransition", yScale=5, xScale=5, time=3000 } )

Hi.  The last line of your code isn’t making any sense to me.  The first argument needs to be a display object, not a function.

local obj = display.newCircle( ... ) transition.to( obj, { x = 100, time = 1000} ) 

Also, I take it that gridGame is a table of tables?  By setting ‘m = 15’, you don’t do anything programatically except assign 15 to the temporary variable ‘m’.  Lua does not have ‘pointers’, only references.

for k,v in pairs(gridGame) do for l,m in pairs(v) do -- At this point, m contains a reference to a table m = 15 -- At this point m contains 15, the table is unchanged -- m is a variable, not a pointer end end

Maybe this is what you want?

-- This function assumes that your grid has this stucture -- --[[aGrid = { { objA, objB, objC, ..., }, ... { obj?, obj?, obj?, ..., }, } local testObj = aGrid[1][1] -- testObj is assigned objA local testObj = aGrid[1][3] -- testObj is assigned objC --]] function gridtransition( aGrid ) for k,v in pairs( aGrid ) do for l,m in pairs(v) do transition.to( m , { ... your parameters here } ) end end end

On a final note, I find multi-dimensional tables to be a terrible way to track objects.  Better, figure out their row and column using their positions instead.  However, as I’ve said in other posts, “to each his or her own”.

Hi again.  It occurred to me that you are storing values in your array.  Then you can change values like this:

-- This example assumes that your grid has this stucture -- --[[aGrid = { { 10, "dog", 20, ..., }, { 15, "god", 3.1415, ..., }, ... } print( aGrid[1][2], aGrid[2][3] ) -- prints: dog 3.1415 --]] for k,v in pairs( aGrid ) do for l,m in pairs(v) do -- at this point m contains a value retrieved from v[l] -- k and l are table 'keys' -- v and m are values at a position in the tables 'aGrid' and v respectively v[l] = 12 -- Now, the value at v[l] has been replaced with 12, while the temporary value in m is the same. end end end

thanks,

I must admit that at first I did not understand at all  :frowning: but reading and rereading I finally understand, thank you for the explanation :slight_smile:
I like this little phrase:
nothing is difficult to understand, you just have to understand.