Quickly find a specific Entry in a massively long Table
With Lua, you can make an ipairs function to iterate through a list until it finds the right entry, then return that entry (or something on the same line, like another property of that entry) - like this:
This makes it very convenient to get for the "name" of an item from a given id - it is a lot better to use the first-capitalized "name" of an item for the description, rather than just use the non-capitalized id. I have been using a similar function which makes it very easy to give a proper description to a large number of items to be registered, when only given the id of an entry in the table of all types (specifically, the "material id" is used to find the "material name", which is passed on to the part of the function which concatenates that to the rest of the name of the item being registered - like "<material name> Pickaxe", for example)
However, when the list is extremely long - such as over 100 entries - I can understand that it would put an enormous load on the processor to iterate through the entire list, looking for one specific entry by checking every single entry, and starting over with the next entry if the previous one does not have the specified id.
I would like to use a more efficient table searching algorithm, which takes significantly less time than the current one (although the time is only taken during the loading stage because these functions are never used during the "running" stage of the server, it is still an enormous load at the stage where it is used). My primitive understanding of Lua does not permit me to readily create an algorithm that goes directly to the specified entry with the given id, although I can understand kinda how such an algorithm should work (and kinda how it would be written too) - however, logic says that even such a cleaner algorithm (which avoids using ipairs to iterate to each entry, checking if it matches the id, and going to the next if it doesn't) would still be just about as resource-hogging as the existing one, because it would have to check each entry by the id before it finds the right one.
Is there something I am missing, or is the currently used method "as quick as it gets"? (specifically, is there a way to go directly to the entry by the specified id, without the program itself having to check each entry until it finds the one which is specified?)
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
table = {
{"red","Red"},
{"green","Green"},
{"blue","Blue"},
}
function <modid>.list_get_name(id,name)
for _, row in ipairs(table) do
column_1 = row[1]
column_2 = row[2]
if column_1 = id then
return column_2
end
end
end
This makes it very convenient to get for the "name" of an item from a given id - it is a lot better to use the first-capitalized "name" of an item for the description, rather than just use the non-capitalized id. I have been using a similar function which makes it very easy to give a proper description to a large number of items to be registered, when only given the id of an entry in the table of all types (specifically, the "material id" is used to find the "material name", which is passed on to the part of the function which concatenates that to the rest of the name of the item being registered - like "<material name> Pickaxe", for example)
However, when the list is extremely long - such as over 100 entries - I can understand that it would put an enormous load on the processor to iterate through the entire list, looking for one specific entry by checking every single entry, and starting over with the next entry if the previous one does not have the specified id.
I would like to use a more efficient table searching algorithm, which takes significantly less time than the current one (although the time is only taken during the loading stage because these functions are never used during the "running" stage of the server, it is still an enormous load at the stage where it is used). My primitive understanding of Lua does not permit me to readily create an algorithm that goes directly to the specified entry with the given id, although I can understand kinda how such an algorithm should work (and kinda how it would be written too) - however, logic says that even such a cleaner algorithm (which avoids using ipairs to iterate to each entry, checking if it matches the id, and going to the next if it doesn't) would still be just about as resource-hogging as the existing one, because it would have to check each entry by the id before it finds the right one.
Is there something I am missing, or is the currently used method "as quick as it gets"? (specifically, is there a way to go directly to the entry by the specified id, without the program itself having to check each entry until it finds the one which is specified?)