First and Second entry in table are consistently swapped

I’m trying to make a table in code, where data from a larger table is condensed into a smaller and more narrow table, however the first entry of the small table is consistently from the second entry of the main table.

local afflictions = {
	test1 = {
		identifier = "test1",
		maxIntensity = 100,
		wholeBody = true,
		effect = {
			consciousness = -1
		}
	},
	
	test2 = {
		identifier = "test2",
		maxIntensity = 100,
		wholeBody = false,
		effect = {
			test1 = 1
		}
	},
	
	test3 = {
		identifier = "test3",
		maxIntensity = 100,
		wholeBody = false,
		effect = {
			test1 = 1
		}
	},
	
	test4 = {
		identifier = "test4",
		maxIntensity = 100,
		wholeBody = false,
		effect = {
			test1 = 1
		}
	}
}

local playerAfflictions = {}
local identifiers = {}

for i, a in pairs(afflictions) do
	table.insert(identifiers, a.identifier)
end

for i, e in pairs(identifiers) do
	print(i.." / "..e)
end

At the end there is a for loop for debugging. I have tried switching them around and the issue still persisted. I’m pretty lost and would appreciate any assistance. Thanks!

1 Like

That’s because dictionaries can’t have a specific order. You would have to manually make a table specifying the order.

local order = {
    'test1';
    'test2';
    'test3';
    'test4';
}
for i,v in ipairs(order) do
    table.insert(identifiers, afflictions[v].Identifier
end

Or, you can just remove the index:

local afflictions = {
   {
		identifier = "test1",
		maxIntensity = 100,
		wholeBody = true,
		effect = {
			consciousness = -1
		}
	},
	
	{
		identifier = "test2",
		maxIntensity = 100,
		wholeBody = false,
		effect = {
			test1 = 1
		}
	},
	
	{
		identifier = "test3",
		maxIntensity = 100,
		wholeBody = false,
		effect = {
			test1 = 1
		}
	},
	
	{
		identifier = "test4",
		maxIntensity = 100,
		wholeBody = false,
		effect = {
			test1 = 1
		}
	}
}
for i,v in ipairs(afflictions) do
    table.insert(identifiers, v.indentifier)
end
1 Like