Jump to content




counter


  • You cannot reply to this topic
2 replies to this topic

#1 Kazimir

  • Members
  • 90 posts
  • Locationthat's no moon...

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)


#2 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

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


#3 Kazimir

  • Members
  • 90 posts
  • Locationthat's no moon...

Posted 17 November 2014 - 07:40 AM

Thank you!





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users