Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Pass table variables to another table
Started by renatocassino Apr 23 2019 11:24 AM

2 replies to this topic
table merge
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

renatocassino

[GLOBAL: userInfoPane.html]
renatocassino
  • Observer

  • 1 posts
  • Corona SDK

Hello guys.
 
I have a table with 72 indexes. All index is an "object" with attributes.
One of theese attributes is a sprite
 
I have two tables: blockList and extraLine
 
The blockList has 72 indexes and extraLine has only 6.
I need remove last 6 of 72, and "prepend" theese 6 in `extraLine` table to blockList.
 
After this, remove all items in extraLine.
 
How can I do this?
 
I'm trying this:

 

function Board:addExtraLine()
 
  for i=72, 7, 1 do
    self.blockList[i] = self.blockList[i-6]
    self.blockList[i-6].y = self.blockList[i-6].y + 1
  end
 
  -- all works until here
  for i=1, 6, 1 do
self.blockList[i].value = self.extraLine[i].value
self.blockList[i].y = 1
  end
 
  self.extraLine = {}
 
end
 
How can I do this?


[TOPIC: post.html]
#2

Alan PlantPot

[GLOBAL: userInfoPane.html]
Alan PlantPot
  • Contributor

  • 915 posts
  • Corona SDK

Before anything else, I noticed that the first loop is incorrect and wouldn't do anything:

for i=72, 7, 1 do

It's attempting to count up from 72 to 7, rather than counting down. The last arg needs to be -1 :

for i=72, 7, -1 do

or flip the 72 and 7

for i=7, 72, 1 do

I'm not sure exactly what you are actually trying to do. You said "I need remove last 6 of 72" but the loop seems to be shifting everything down by 6, thereby removing the first 6 entries. 

 

If you need to remove entries at the start and have all of the other entries adjust automatically to close any "gaps" in the table, just use table.remove() in a loop (and always loop backwards when doing this otherwise the previous removal will cause the indices to mismatch on the next removal):

for i= 6, 1, -1 do
    table.remove(blockList[i])
end

If you really did mean you want to remove the entries at the end and the initial loop in your sample was wrong, just change the loop indices:

for i= #blockList, #blockList-6, -1 do
    table.remove(blockList[i])
end

(Note: #blockList means "the length of the blockList table". Using #blockList instead of "72" means that if you ever decide to change the length of this table from 72,  you won't need to go back and change the number 72 in lots of places in your code. It's worth getting into the habit of using the # for getting the length of the table.)

 

Then if you want to add new entries, you can force them into any index in the table using table.insert:

for i= 1, 6 do
    table.insert(blockList, i, extraLine[i])
end
This will insert extraLine[1] into position blockList[1] , and move any existing entries up so the previous blockList[1] gets bumped up to blockList[2]. Then extraLine[2] into position blockList[2], and so on.


[TOPIC: post.html]
#3

Yoger Games

[GLOBAL: userInfoPane.html]
Yoger Games
  • Contributor

  • 109 posts
  • Corona SDK

I think the moses library would come in handy here. 

 
unshift (array [, n = 1])
Removes and returns the last value in an array.
 
prepend (array, ...)
Adds given values at the top of an array, preserving the order at which elements are passed-in.
In your case it would be something like
local values = moses.unshift(blockList, 6)
moses.prepend(extraLine, values)



[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: table, merge