get the key of the smallest value in a table ?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Vrx8
Prole
Posts: 20
Joined: Sat Jun 17, 2017 5:35 am

get the key of the smallest value in a table ?

Post by Vrx8 »

confused.. the values in the table are constantly changing.. and i want to get the key of the smallest value in the table, how do i do it ? googled it out.. find nothing
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: get the key of the smallest value in a table ?

Post by Nixola »

Iterate over the whole table, keeping track of the lowest value.

Code: Select all

function returnMin(t)
  local k
  for i, v in pairs(t) do
    k = k or i
    if v < t[k] then k = i end
  end
  return k
end
EDIT: fixed code, as pointed by grump
Last edited by Nixola on Sat Sep 09, 2017 1:08 pm, edited 1 time in total.
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Vrx8
Prole
Posts: 20
Joined: Sat Jun 17, 2017 5:35 am

Re: get the key of the smallest value in a table ?

Post by Vrx8 »

Nixola wrote: Sat Sep 09, 2017 12:32 pm Iterate over the whole table, keeping track of the lowest value.

Code: Select all

function returnMin(t)
  local k
  for i, v in pairs(t) do
    k = k or i
    if v < t[k] then k = i
  end
  return k
end
have you try it out ?
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: get the key of the smallest value in a table ?

Post by grump »

There's an 'end' missing in Nixola's code.

Code: Select all

function returnMin(t)
  local k
  for i, v in pairs(t) do
    k = k or i
    if v < t[k] then k = i end -- end was missing here
  end
  return k
end
Vrx8
Prole
Posts: 20
Joined: Sat Jun 17, 2017 5:35 am

Re: get the key of the smallest value in a table ?

Post by Vrx8 »

grump wrote: Sat Sep 09, 2017 1:06 pm There's an 'end' missing in Nixola's code.

Code: Select all

function returnMin(t)
  local k
  for i, v in pairs(t) do
    k = k or i
    if v < t[k] then k = i end -- end was missing here
  end
  return k
end
still not working for me.. idk why, but I found out my own way..
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: get the key of the smallest value in a table ?

Post by Nixola »

Vrx8 wrote: Sat Sep 09, 2017 1:13 pm still not working for me.. idk why, but I found out my own way..
Could you please share how and why it did not work, and what your own way is?
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: get the key of the smallest value in a table ?

Post by zorg »

Vrx8 wrote: Sat Sep 09, 2017 1:13 pm
grump wrote: Sat Sep 09, 2017 1:06 pm There's an 'end' missing in Nixola's code.

Code: Select all

function returnMin(t)
  local k
  for i, v in pairs(t) do
    k = k or i
    if v < t[k] then k = i end -- end was missing here
  end
  return k
end
still not working for me.. idk why, but I found out my own way..
Nixola wrote: Sat Sep 09, 2017 1:14 pm
Vrx8 wrote: Sat Sep 09, 2017 1:13 pm still not working for me.. idk why, but I found out my own way..
Could you please share how and why it did not work, and what your own way is?
Or in other words, please be as helpful as we tried to be towards you. :ehem:
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
Sir_Silver
Party member
Posts: 286
Joined: Mon Aug 22, 2016 2:25 pm
Contact:

Re: get the key of the smallest value in a table ?

Post by Sir_Silver »

One reason it may not work for him is because it breaks if one of the values in the table is not a number. Like with this table:

Code: Select all

local table = {
	1,
	2,
	3,
	"elephant"
}
Try this instead:

Code: Select all

local function get_smallest_number(table)
	local smallest_number
	
	for k, v in pairs(table) do
		if type(v) == "number" then
			if not smallest_number then
				smallest_number = v	
			end
			
			smallest_number = v < smallest_number and v or smallest_number
		end
	end
	
	return smallest_number
end

local table = {
	1,
	2,
	"buckle_my_shoe",
	function() end,
	-69, --smallest number in the table
	{{}}
}

print(get_smallest_number(table))  --prints -69
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: get the key of the smallest value in a table ?

Post by ivan »

Nixola's solution is the nicest, but I have a small suggestiion:

Code: Select all

function table.min(t)
  local k, v = next(t)
  for k2, v2 in pairs(t) do
    if v2 < v then
      k, v = k2, v2
    end
  end
  return k, v
end
Just make sure the table contains strictly numeric values, otherwise it doesn't make sense.
PS.A more crazy solution for numerically-indexed tables could be to use something like table.sort or math.min(unpack(t)).
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: get the key of the smallest value in a table ?

Post by Nixola »

Table.sort and math.min solutions are not what's required, since he needs the key; it is true that my function will error in case something's not a number, and it would require several added checks.
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Post Reply

Who is online

Users browsing this forum: No registered users and 32 guests