get the key of the smallest value in a table ?
get the key of the smallest value in a table ?
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
Re: get the key of the smallest value in a table ?
Iterate over the whole table, keeping track of the lowest value.
EDIT: fixed code, as pointed by grump
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
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
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Re: get the key of the smallest value in a table ?
have you try it out ?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
Re: get the key of the smallest value in a table ?
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
Re: get the key of the smallest value in a table ?
still not working for me.. idk why, but I found out my own way..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
Re: get the key of the smallest value in a table ?
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
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
- zorg
- Party member
- Posts: 3444
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: get the key of the smallest value in a table ?
Vrx8 wrote: ↑Sat Sep 09, 2017 1:13 pmstill not working for me.. idk why, but I found out my own way..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
Or in other words, please be as helpful as we tried to be towards you.
Me and my stuff True 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.
- 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 ?
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:
Try this instead:
Code: Select all
local table = {
1,
2,
3,
"elephant"
}
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
Re: get the key of the smallest value in a table ?
Nixola's solution is the nicest, but I have a small suggestiion:
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)).
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
PS.A more crazy solution for numerically-indexed tables could be to use something like table.sort or math.min(unpack(t)).
Re: get the key of the smallest value in a table ?
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
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Who is online
Users browsing this forum: No registered users and 255 guests