Jump to content
  • 0

Counting amount of times a value appears in table


nok1a

Question

I want to have script that takes table from the result list. Then prints out the amount of times a value appears. If the value has already been counted once it must continue to the next index. I have this script but it also counts the values that already have been counted.

local count = {}
local t = {}
gg.searchNumber("3D;8D;4D::17", gg.TYPE_DWORD) -- random search
count = gg.getResultsCount()
t = gg.getResults(count)
for i = 1, #t do
  loop = 0
  for b, w in ipairs(t) do
    if t[i].value == w.value then
      loop = loop + 1
    end
  end
  print(t[i].value, loop)
end

 

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

15 hours ago, nok1a said:

I want to have script that takes table from the result list. Then prints out the amount of times a value appears. If the value has already been counted once it must continue to the next index. I have this script but it also counts the values that already have been counted.

local count = {}
local t = {}
gg.searchNumber("3D;8D;4D::17", gg.TYPE_DWORD) -- random search
count = gg.getResultsCount()
t = gg.getResults(count)
for i = 1, #t do
  loop = 0
  for b, w in ipairs(t) do
    if t[i].value == w.value then
      loop = loop + 1
    end
  end
  print(t[i].value, loop)
end

 

If I understand correctly and you want it to print out the total number of times a value appears then this should work.

 

local count = {}
local t = {}
gg.searchNumber("3D;8D;4D::17", gg.TYPE_DWORD) -- random search
count = gg.getResultsCount()
t = gg.getResults(count)
local totalsTable = {}
for i,v in pairs (t) do
    if not totalsTable[tostring(v.value)] then
        totalsTable[tostring(v.value)] = 1
    else
        totalsTable[tostring(v.value)] = totalsTable[tostring(v.value)] + 1
    end
end
print(totalsTable)--totals of all values
print(totalsTable["8"])--totals of specified value, make sure you put it in quotes so it is treated as string

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.