2
votes

I am working on a game project using Corona SDK and I am running into an issue. I am trying to use string.find() in a for loop to test to determine if a value is in a certain table and if so, add that value to another table. My problem is that since string.find()/string.match does not read duplicates in this case (assuming that the for loop is the reason why). I am essentially only having "1102", "1103" instead of "1102", "1102", "1103", "1102", in the "copy" table which is how I am trying to get this to do. Any suggestions?

database = 
{
 {name="test", serial="1102", img="src/1.png"},
 {name="test2", serial="1103", img="src/2.png"},
 {name="test3", serial="1104", img="src/3.png"}
}

list = 
{
 "1102",
 "1102",
 "1103",
 "1102"
}

copy = {}
n=1

for i=1, #database do
 if string.find(database[i].serial, tostring(list[n])) then 
   table.insert(copy, database[i].img)
   n=n+1
 end
end    
for i=1, #copy do 
    print(copy[i])
end
2
What is n in tostring(list[n])? You want to match a value in database if its serial matches any of list or a specific one? What do the duplicate elements in list mean? - Yu Hao
Meant to add n=1 so each find is n=n+1 to go to the next index. Basically what I am trying to do is match the serial in 'list' with 'database' and each time it does, add the img value to the 'copy' table. The biggest thing is that I need it do this even for duplicates like the 3 "1102" serials, in the order appearing in 'list'. The copy table is then used to draw the images, even duplicates. I cannot figure this out. I am thinking a for loop but I am not sure how to go about it. - Terry English
With your example input, you expect copy to have one "src/1.png" and one "src/2.png", is this correct? - Yu Hao
The code has it reading as: src/1.png, src/2.png however, it needs to be reading as src/1.png, src/1.png, src/3.png, src/1.png which follows how 'list' is setup. - Terry English
I am stuck on how to traverse through the table this way until list has all of its img put into copy - Terry English

2 Answers

1
votes

Using a nested loop works.

for lk, lv in ipairs(list) do
  for dk, dv in ipairs(database) do
    if string.find(dv.serial, tostring(lv)) then 
      table.insert(copy, dv.img)
    end
  end
end

I'm using ipairs, which is similar to for i=1, #list do.

0
votes

Are the serials in the database table unique? If so, From the code, I think you can make your database table to be more efficient.

local database = 
{
 [1102] = {name="test", img="src/1.png"},
 [1103] = {name="test2", img="src/2.png"},
 [1104] = {name="test3", img="src/3.png"}
}

Note that, with this change, you cannot use iterative for loops to navigate in the database table. However, the part where you do the check becomes this:

local list = {1102,1102,1103,1102,}
local copy = {};
for index, serial in next, list do 
    if database[serial] then copy[#copy+1] = database[serial].img end
end

For the last part, you can use table.concat() to print values of a table as a string, instead of iterating over it one by one:

table.concat(copy, "\n")

And lastly, I know the code you've written up there is an example, but be careful about global variables; I hope they (database, list, copy) are not global in your code. Anyway,

With Regards, lyr