what I actually want is, for example, i have the index and the value of one of the line, say
health = 120, cost = 35000
i want to get the next index so i would get 130. Do you know how to get (130) from (120) or get (130) from (35000) ?
EDIT: I want to be able to do this without adding repeated codes. In my previous game i had another whole dictionary that basically has the next index. But that is WAY too complicated. Thank you so much for helping please give me a solution thx
Dictionaries have no order, there is no way to go through them in order in that format without any of the solutions other people have listed above, sorry
Use @D0RYU’s solution. That will work perfectly in your case
Thank you so much! But I still have a lot of dictionaries that have to be converted into an array of dictionaries. Are you kind enough to help me out a bit please? To contact me, you can join my discord server here. Thank you so much again! Have a nice day!
Thank you so much! But I still have a lot of dictionaries that have to be converted into an array of dictionaries. Are you kind enough to help me out a bit please? Here is an example:
Don’t convert it to an array, just use this function…
module.healths = {
["100"] = 5000;
["110"] = 15000;
["120"] = 35000;
["130"] = 50000;
["145"] = 75000;
["160"] = 125000;
["185"] = 300000;
["200"] = 475000;
["225"] = 750000;
["255"] = 1050000;
["280"] = 1350000;
["320"] = 1850000;
...
}
local function getKeys(t)
local keys = {}
for k in next, t do
table.insert(keys, k)
end
table.sort(keys)
return keys
end
for _, k in ipairs(getKeys(module.healths)) do
local v = module.healths[k]
print(k, v)
end
you might be wondering, why doesn’t this work with all dictionaries?
well the function sorts alphanumerically, this means that it will work if the number keys are in order or if the text is in alphabetical order
you also can’t have a string and a number as a key in the same table, this is because you can’t compare string and number
anyways since it sorts alphanumerically stuff like this also won’t work right
I slightly modified a code sample I wrote a few months ago, for your specific situation:
local dictionary = { -- the dictionary in question to be looped through
-- these are example values to be sorted properly
['110'] = 100011001,
['105'] = 20293922,
['230'] = 923929932,
['c'] = 292382,
['a'] = 29382393128,
}
local function loopDictionaryInOrder(dict, func)
local data = {} -- array to hold the indexes
for key, value in pairs(dict) do -- loops though the dictionary provided in the first parameter
table.insert(data, key) -- this adds each key to the 'data' array
end
table.sort(data, function(a, b) -- this sorts the data array by each key's byte (numeric representations)
local toNumA = tonumber(a)
local toNumB = tonumber(b)
if toNumA and toNumB then
-- this checks for 2 integer values and sorts them accordingly
return toNumA < toNumB
end
return tostring(a):lower():byte() < tostring(b):lower():byte()
end)
for _, key in ipairs(data) do
-- loops through the 'data' array and calls the function provided in the second parameter
-- the function gives the key as the first argument and the key's value as the second argument
func(key, dict[key])
end
end
-- this is an example usage of the function
loopDictionaryInOrder(dictionary, function(key, value)
-- print each key and value in order
print(key, value)
end)
I can’t see if this works in Roblox Studio because Roblox is down at the moment, but it works on Lua’s Demo Site.
The regular sort provides the same result in this case.
You could also use the function as an iterator function, which I think is pretty cool:
local function idict(t)
local keys = {}
for k in next, t do
table.insert(keys, k)
end
table.sort(keys)
return function(_, i)
i += 1
local k = keys[i]
if k then
return i, k, t[k]
end
end, keys, 0
end
for _, k, v in idict(module.healths) do
print(k, v)
end
Thank you very much for all your replies! I should have edited the title, but what I actually want is a function that returns the next index of the given index. Can someone help me with making a function that returns the next index of a given index please? Like for example,
module.NextHealth = function(currentHealth)
-- current health is 145
-- I want to get 160 because it is the next index
-- if currentHealth is already the last index, return the string "MAX"
end
local nextIndex, nextValue = next(table, currentIndex)
Bear in mind, as I’ve mentioned, that dictionaries don’t really have an order. I think this method does work on arrays and in order, but if it doesn’t then this is all you need.
local function arrayNext(tab, key)
return key+1, tab[key+1] or "MAX"
end