title: Check 2d array for same values
body: <p>I am trying to make a game and i have a 2d array</p>
  
  <p>So its like this:</p>
  
  <pre><code>Grid[x][y]
  </code></pre>
  
  <p>lets pretend these values are in it:</p>
  
  <pre><code>Column 1 Column 2 Column 3 Column 4 Column 5
  1        2        5        2        5 
  2        2        3        1        1 
  1        4        3        4        5 
  1        3        3        3        5  &lt;-- match this row
  3        5        3        4        5 
  2        4        3        4        5
  2        4        4        4        5 
  </code></pre>
  
  <p>In the middle (index 4) i want to check if there are at least 3 times the same number and what about if there are 4 times the same or even 5.</p>
  
  <p>How do you check this ? What would be a good way to find the same and delete those that are the same... I am stuck to figure out the logic to make something like this</p>
  
  <p>this is what i tried: </p>
  
  <pre><code>grid = {}
  for x = 1, 5 do
    grid[x] = {finish = false} 
    for y = 1, 7 do 
      grid[x][y] = {key= math.random(1,4)}
    end 
  end
  
  function check(t)
    local tmpArray = {}
    local object
    for i = 1,5 do
      object = t[i][1].key
      if object == t[i+1][1].key then
        table.insert( tmpArray, object )
      else 
        break
      end
    end
  end
  
  print_r(grid)
  check(grid) 
  print_r(grid)
  </code></pre>
  
  <p>where <code>print_r</code> prints the grid:</p>
  
  <pre><code>function print_r ( t ) 
      local print_r_cache={}
      local function sub_print_r(t,indent)
          if (print_r_cache[tostring(t)]) then
              print(indent.."*"..tostring(t))
          else
              print_r_cache[tostring(t)]=true
              if (type(t)=="table") then
                  for pos,val in pairs(t) do
                      if (type(val)=="table") then
                          print(indent.."["..pos.."] =&gt; "..tostring(t).." {")
                          sub_print_r(val,indent..string.rep(" ",string.len(pos)+8))
                          print(indent..string.rep(" ",string.len(pos)+6).."}")
                      else
                          print(indent.."["..pos.."] =&gt; "..tostring(val))
                      end
                  end
              else
                  print(indent..tostring(t))
              end
          end
      end
      sub_print_r(t,"  ")
  end
  </code></pre>
  
  <p>It doesnt work that great because i check with the index after that one and if that isnt the same it doesnt add the last one..
  I dont know if it is the best way to go...</p>
  
  <p>If i "delete" the matched indexes my plan is to move the index row above or beneath it into the 4 index row... but first things first </p>