How to copy a table, but only with specific entries?

I'm making a copy of a table, and I want the new table to only use a specific subset of the original. What I'm not sure is how that would work in LUA language?

1
2
3
4
local newTable={}
for i=1,50 and otherTable[i].Job="Shopkeep" do
newTable[i]= { name=otherTable[i].Name, Job="Shopkeep" }
end

i think it should be as below..

1
2
3
4
5
6
local newTable={}
for i=1,50,1 do
    if otherTable[i].Job == "Shopkeep" do
         newTable[i]= { name=otherTable[i].Name, Job="Shopkeep" }
     end
end

Trying to figure out if I can adapt your code...

for i=1,50,1 do
Why specify three numbers? (Admittedly my syntax knowledge of 'for' is terrible...)

if otherTable[i].Job == "Shopkeep" do

'do' crashes the build. 'then' seems to work in it's place.

Apart from that, I'm still trying to troubleshoot getting this to work. That being said, I think that the widgets do not tolerate missing spaces in a table (ie: [1][2][3] are fine but [1][3][5] are not) so I guess I need to find another approach where I can fill all of the slots of the new table.

ie: if given this table;

1
2
3
otherTable[1] = { name="Jim", Job="Hunter"}
otherTable[2] = { name="Joe", Job="Drinker"}
otherTable[3] = { name="Jed", Job="Drinker"}

sorry that was a typo...

the for loop should have 3 numbers but last number will default to 1 if not given so it will work with 2 values also. 3rd value specify the increment.
ie if we give 2 it will increment by 2

1
2
3
for 1=1,9,2 do
  print(i)
end

Yeah! That works. I had something similar but just couldn't get the [j]'s and [i]'s on the same page.

However, it had one major bug left which I just figured out. Nothing wrong with your code example, but something for anybody listening that wants to use widget.newTableView()

1. Using the above code you would now have newTable{}. But you can't tap anything without having an onRelease=command entry in each table ID!

2. Okay, so I'll just add the onRelease entry. Sorry! if you do this:

onRelease = PressingtheButton

..The table generator will just fill in nil as the entry. This means you need to have your onRelease function defined first. This is a problem, particularly because if you're developing anything on the iPad, that function NEEDs to know the table is there first. (I tried declaring the table first but that didn't seem to be enough.)

3. To get past these conflicting problems, you need to use this order:
a. Define the table
b. Define the onRelease function
c. Inject the onRelease entry into the table.

Example:

1
2
3
for i=1,table.maxn(newTable) do
newTable[i].onRelease = PressingtheButton
end
views:1855 update:2011/9/27 8:54:05
corona forums © 2003-2011