Home > Net >  Array return are sort in reverse and i dont know why
Array return are sort in reverse and i dont know why

Time:12-10

I have a weird problem that I can't solve since yesterday on Garry's Mod (GLua)
When my gmod server game is running, I notice that there are errors on arrays that are empty with certain keys when they are well specified, while doing deep tests, I noticed that the returned arrays were ... backwards.

Here is an array below:

bigArray = {
    [ "default" ] = { 4, 2, 1 },
    [ "police" ] = { 4, 2, 1 },
    [ "mayor" ] = { 5, 2, 1 },
    [ "sherif" ] = { 6, 2, 1 },
}

Good, next I will use the PrintTable() method (PrintTable() is a method already integrated in the game) which displays the contents of an array (This method, normally if I run PrintTable(bigArray) the result should be literally the array above, but here is the result displayed:

{
    [ "sherif" ] = { 6, 2, 1 },
    [ "mayor" ] = { 5, 2, 1 },
    [ "police" ] = { 4, 2, 1 },
    [ "default" ] = { 4, 2, 1 },
}

I will put an example more telling since the previous one is an dictionary and not really an array :

table = {
   'truc',
   'machin',
   'chose'
   }

If I display the first element of the table like this print(table[1]), the displayed result will be: chose

Flipping the tables upside down makes a lot of addons I use crash, I have no idea how this happened, it happened suddenly without me modifying any addon (I already looked at the worshop addons, none of them are responsible for the problem) If someone has an idea how this could be caused, I'm interested, thanks.

CodePudding user response:

I noticed that the returned arrays were ... backwards

They are not arrays. They are dictionaries (unordered set of key-value pairs).

An array in Lua would look like the following:

bigArray = {
    { name="default", 4, 2, 1 },
    { name="police",  4, 2, 1 },
    { name="mayor",   5, 2, 1 },
    { name="sherif",  6, 2, 1 },
}

In this case order of elements is preserved:

$ lua
Lua 5.4.4  Copyright (C) 1994-2022 Lua.org, PUC-Rio
> bigArray = {
    { name="default", 4, 2, 1 },
    { name="police",  4, 2, 1 },
    { name="mayor",   5, 2, 1 },
    { name="sherif",  6, 2, 1 },
}
> bigArray[1].name # This will _always_ be "default"
default 

CodePudding user response:

In Garry's Mod dictionaries are not stored in any particular order. If you want to iterate through a dictionary in order, rather than using pairs you must use either SortedPairs, SortedPairsByMemberValue or SortedPairsByValue. See this wiki page for reference.

For your implementation, I would recommend adding a field to each member value of your bigArray dictionary to specify a sort order; for instance:

local bigArray = {
    [ "default" ] = {
        sortOrder = 1,
        myValues = { 4, 2, 1 }
    },
    [ "police" ] = {
        sortOrder = 2,
        myValues = { 4, 2, 1 }
    },
    [ "mayor" ] = {
        sortOrder = 3,
        myValues = { 5, 2, 1 }
    },
    [ "sherif" ] = {
        sortOrder = 4,
        myValues = { 6, 2, 1 }
    }
}

This would then allow you to iterate in order of the sortOrder value, like so:

for key, value in SortedPairsByMemberValue(bigArray, "sortOrder") do
    print("\n" .. key .. ":")
    PrintTable(value)
end
  • Related