This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Kazimir's profile picture

counter

Started by Kazimir, 17 November 2014 - 06:28 AM
Kazimir #1
Posted 17 November 2014 - 07:28 AM
Help me to make the counter recurring items.


tE = {'c', 'c', 'c', 'd', 'a', 'b'}

tC = {['a'] = 0, ['b'] = 0, ['c'] = 0, ['d'] = 0}

function counter(tbl)
  for zr = 1, #tbl+1 do
	tC[tbl[zr]] = 1
  end

  for o = 1, #tbl+1 do
	i=o+1
	while i < #tbl+1 do
	  if tbl[o] == tbl[i] then
		-- what to write, to the values in the tC was correct?
	  end
	  i=i+1
	end
  end
end

counter(tE)
Bomb Bloke #2
Posted 17 November 2014 - 07:35 AM
local function counter(tbl)
	local tC = {}
	
	for zr = 1, #tbl do
		if tC[tbl[zr]] then
			tC[tbl[zr]] = tC[tbl[zr]] + 1
		else
			tC[tbl[zr]] = 1
		end
	end
	
	return tC
end

local tE = {'c', 'c', 'c', 'd', 'a', 'b'}

local amounts = counter(tE)

for key, value in pairs(amounts) do
	print(key..": "..value)
end
Kazimir #3
Posted 17 November 2014 - 07:40 AM
Thank you!