Jump to content

Recommended Posts

hi

so for the purpose of checking whether a table is composed of ONLY unique items I instead remove duplicates into a 2nd table & check whether or not the tables are of equal size

local places = { GetModConfigData("Placement1Conf"), GetModConfigData("Placement2Conf"), GetModConfigData("Placement3Conf"),
    GetModConfigData("Placement4Conf"), GetModConfigData("Placement5Conf"), GetModConfigData("Placement6Conf"),
    GetModConfigData("Placement7Conf") }

local checkduplicate = function()
    print("checkingforduplicates")
    local unique = {}
    for i=1,#places do
        if unique[places[i]] == nil then
            unique[places[i]] = true
        end
    end
    print(#places)
    print(#unique)
    if #places ~= #unique then -- this verifies that all of places table are unique items
        sort_custom = false
        return 1 --means every item in places is NOT unique
    else
        return 5 --means every item in places is unique
    end
end

now the log

../mods/SortInventory Altered/modmain.lua(26,1) checkingforduplicates	
../mods/SortInventory Altered/modmain.lua(33,1) 7	
../mods/SortInventory Altered/modmain.lua(34,1) 0

which means the unique table is NOT getting filled and I don't understand why. I also tried : if not unique[places] then

 

and here's the modinfo

	{
		name = "Placement1Conf",
		label = "First group of items",
		options =
		{
			{description = "Weapons", data = "w"},
			{description = "Lights", data = "l"},
			{description = "Tools", data = "t"},
			{description = "Books", data = "b"},
			{description = "Equips", data = "e"},
			{description = "Foods", data = "f"},
			{description = "Others", data = "o"},
		},
		default = "w",
	},

  all are the same pretty much, which is why I need to check for dupes.

Edited by whismerhill

Hmm... It looks like it should work. I suggest you add 'print' calls  in every iteration of the loop, telling you the value of 'places' and the result of the conditional.

 

Also, it may be more optimal to iterate using 'for i,v in ipairs(places)', which will set 'v' to 'places' for each iteration.

Hi,

thanks Arkathorn. I actually changed 2 things not sure which one did the trick but now the 2nd table is filled by 7 entries !!!! yeah !!! xD

(used table.insert instead of the previous method)

 

    for i,v in ipairs(places) do -- equivalent of for i=1,#places do v = places[i]
        print(v)
        if unique[v] == nil then
            --unique[places[i]] = true
            table.insert(unique,v)
        end
    end

 

and I rejoiced too soon lol. table.insert(unique,v), actually inserts v (so places) as a value instead of a key ....  (so it actually insert all the 7 values & the size of the resulting table will always be 7)

table.insert(unique,v,true) complains that the key is a string instead of an integer .... (of course)

unique[v] = true or unique.v = true don't seem to work any better (no insertions at all, so table size 0 all the time)

I alread tested out that it's going inside the condition, so no need to check that. the problem lies in the insertion of entries

Edited by whismerhill

#unique returns the number of number-indexed entries on the table.

If unique is { hello = 1, world = 2 } then #unique is 0.

You need to iterate with pairs.

local unique_size = 0
for k, v in pairs(unique) do
	unique_size = unique_size + 1
end

 

24 minutes ago, whismerhill said:

and I rejoiced too soon lol. table.insert(unique,v), actually inserts v (so places) as a value instead of a key ....  (so it actually insert all the 7 values & the size of the resulting table will always be 7)

table.insert(unique,v,true) complains that the key is a string instead of an integer .... (of course)

unique[v] = true or unique.v = true don't seem to work any better (no insertions at all, so table size 0 all the time)

I alread tested out that it's going inside the condition, so no need to check that. the problem lies in the insertion of entries

LUA recap on tables:

table[key] = value

The size of a table can only nicely be calculated if the table is in an ipair setup.

Which is to say the table needs to have numerical keys to their values.

 

To check for if a table only has unique values, you may do:

function isunique(input)
    local cache = {}
    for k, v in pairs(input)
    do
        if(cache[v])
        then
            return false
        end
        cache[v] = true
    end
    return true
end

For unique keys, just change the cache[v] to cache[k] for both instances.

 

While I'm not sure what you're trying to do, it sounds like it's not going to be the best way of doing it.

DarkXero thanks. that explains it xD

CarlZalph thanks, seems like your implementation is more efficient than mine.Although I doubt efficiency is gonna really be that much important given the small scope of that function usage, I'll still use it, whatever's more efficient should be used as a general rule and as long as it doesn't impact negatively maintenance imho.

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
×
  • Create New...