Counting elements in nested table by name starting with

Viewed 32

I have the table listed below

raids = {
    {
        T1I0 = {
            {'Mightstone of Sargaras', 'Mightstone of Sargaras\n\nMightstone of Sargaras is obtained by farming. GL'}
        },
        T1I1 = {
            {'Blessings Jewel of Elune', 'test'}
        },
        T1I2 = {
            {'Lifegiving Gem of Amanthel', 'test'}
        },
        T2I0 = {
            {'Practicing monster pot', 'test'}
        },
        T2I1 = {
            {'Nuwa stone', 'test'}
        }
    }
}

I've managed to count the amount of elements T1I0 -> T2I1 = 5 by using the function below, combined with tablelength(raids[1])

function tablelength(T)
  local count = 0
  for _ in pairs(T) do count = count + 1 end
  return count
end

But I'm having some problem with counting only elements starting with T1, sound return 3.

Anyone got an ide how I can modify the last part?

1 Answers

Try this code:

  for k in pairs(T) do if k:sub(1,2)=="T1" then count = count + 1 end end

However, consider re-structuring the table in two layers, the first one having keys T1 and T2. After this, the count you want is simply #raids.T1.

Related