Jump to content

How to get the smallest key from a table?


N3xT

Recommended Posts

Hey everyone, as you see is it possible to get the smallest key from a table? example:

local exampleTable = {
  -- 5
  -- 6
  -- 7
  -- 8
  -- 9
  -- 10
}

I know if I want the last key I could use that:

if key == #exampleTable then
  	--

But what about the first key ( 5 ), Thanks in advance.

Edited by N3xT
Link to comment

I'm unsure which method is faster or more optimised, but you can do either

function getLowestValue(haystack)
  assert(haystack and type(haystack) == "table", "Bad argument @ 'getLowestValue' [Expected 'table' at argument 1, got '"..type(haystack).."']")
  local lowest, thekey = math.huge, nil -- set lowest to value of infinity
  for key, value in pairs(haystack) do
    if value < lowest then -- if this value is lower than the lowest value found so far (infinity to begin with)
      lowest, thekey = value, key -- set that lowest value to this value and store the key/index where we found the value
    end
  end
  return lowest, thekey -- return the lowest value found along with the key/index under which it was found (nil if there was nothing lower than infinity)
end

or try using table.sort to sort the table by value from lowest to highest (not sure if this works for tables with non-integer/non-sequential keys/indices) and simply access the first index of that table. Table.sort is part of Lua's standard libraries, which means it is implemented in C. Both methods definitely iterate the table, and I've heard that in some instances, table.sort can be slower than other sorting algorithms written in pure Lua.

Edited by MrTasty
  • Like 1
Link to comment
5 minutes ago, MrTasty said:

I'm unsure which method is faster or more optimised, but you can do either


function getLowestValue(haystack)
  assert(haystack and type(haystack) == "table", "Bad argument @ getLowestValue (1). Expected 'table', got '"..type(haystack).."'")
  local lowest, thekey = math.huge, nil -- set lowest to value of infinity
  for key, value in pairs(haystack) do
    if value < lowest then -- if this value is lower than the lowest value found so far (infinity to begin with)
      lowest, thekey = value, key -- set that lowest value to this value and store the key/index where we found the value
    end
  end
  return lowest, thekey -- return the lowest value found along with the key/index under which it was found (nil if there was nothing lower than infinity)
end

or try using table.sort to sort the table by value from lowest to highest (not sure if this works for tables with non-integer/non-sequential keys/indices) and simply access the first index of that table. Table.sort is part of Lua's standard libraries, which means it is implemented in C. Both methods definitely iterate the table, and I've heard that in some instances, table.sort can be slower than other sorting algorithms written in pure Lua.

Didn't work, and the table is already sorted as the example.

Link to comment

If it's already sorted, you should be able to pick the first value and it should be the lowest, and the last value which would be the highest

sortedTable[1] -- first value, the lowest if sorted properly
sortedTable[#sortedTable] -- last value, the highest if sorted properly

 

Edited by MrTasty
  • Like 1
Link to comment
1 minute ago, MrTasty said:

If it's already sorted, you should be able to pick the first value and it should be the lowest, and the last value which would be the highest


sortedTable[1] -- first value, the lowest if sorted properly
sortedTable[#sortedTable] -- last value, the highest if sorted properly

 

You didn't understand me yet, it's sorted but the first key isn't 1

5

6

7

etc..

The key everytime changes, that's why I need to get the lowest number from the table.

I tried another way and It's working just fine. Thanks, @MrTasty.

Link to comment

Oh. In that case I'd just modify the original code I gave, to select the lowest index rather than value

local lowest = math.huge
for key, value in pairs(sortedTable) do
  if type(key)=="number" and key < lowest then
    lowest = key
  end
end

-- or alternatively, use a loop that doesn't use pairs function

local lowest = 0
while (true) do -- warning: this will loop indefinitely if the table has no indices that aren't numbered
  lowest = lowest + 1 -- keep incremeting the index
  if sortedTable[i] then -- until an index is found with a logically non-false value
	break -- and escape the while-loop
  end
end

-- Now here, 
--   lowest = the lowest number index in the table, and of course, 
--   sortedTable[lowest] = the value of the lowest index of the table

 

Edited by MrTasty
  • Like 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...