sorry about the weird title, I kept getting “title isn’t a complete sentence” message and couldn’t find a way to re-word what I was going to put without the error message
Ok this is a quick question, basically I have a dictionary, that looks something like this:
I’m creating a function to be able to write to the dictionary, and I’m trying to just make it look cleaner. Here’s what my idea was:
I’m passing in 2 values,
Path (The path to the value I want to be written to)
This is a table that would look something like this:
{“ItemType2”, “SubItemType1”, “Item1”}
Value (The value that is written to the given path.)
This is just a number
The goal is to sort of have something like this:
Dictionary[Path[1]][Path[2]][Path[3]] = Value
Well, the problem with this is the length of the path will be different at times. If I want Dictionary[“Item1”] to be written to I’d need it to be:
Dictionary[Path[1]] = value
However, I don’t know how to do this without having the ugly:
if #Path == 1 then
Dictionary[Path[1]] = value
elseif #Path == 2 then
Dictionary[Path[1]][Path[2]] = value
elseif #Path == 3 then
Dictionary[Path[1]][Path[2]][Path[3]] = Value
end
This looks like something that could/should be shortened but I’m not quite sure how.
Not sure if I’m understanding correctly but why why not create a nested table for different items, and then different tables for different ItemTypes? You can also reference the different tables by declaring the variables at the top, then assigning them values later.
local items, itemTypes, subItemTypes
items = {
['Item1'] = {
['ItemType'] = 1; -- or you can do ['ItemType'] = itemTypes[1]
['SubItemType'] = 2; -- same here, ['SubItemType'] = subItemTypes[2]
};
}
Ah, got it. Thanks for that. I’m a little confused as to where you’re having the values reference each other though? You should be able to edit values externally like this:
I’m not sure why you need it like this, but you can use something like this to traverse to an arbitrary Path depth/length:
local function readPath(Dictionary, Path)
for _, v in ipairs(Path) do
if Dictionary[v] then
Dictionary = Dictionary[v]
else
return Dictionary, v .. " is not a valid child of " .. Dictionary.Name -- or some other property!
end
end
return Dictionary
end
local currentPath = PlayersData[key]
for i = 1, #Path do
currentPath = currentPath[Path[i]]
end
print(currentPath) -- nil
print(Value) -- 0
currentPath = Value
print(currentPath) -- 0
end
However it doesn’t seem write to the dictionary, it seems to just overwrite the variable.
That’s why you use a function! (or a do block with a temp value to hold the reference)
What you’ve essentially created is a form of Linked List, so you need to either intentionally shadow the variable in a function, as I did, or create a second variable so you can have both references at once.
EDIT 2: Oh, I misread what you did there. If you’re overwriting the final currentPath[Path[i]] with a value, you have to do that, you can’t set currentPath to the value, it’s the same as trying to do v = a.property; v = 10; --a.property is unchanged
Example:
local function setPath(Dictionary, Path, Value)
for i = 1, #Path - 1 do
local v = Path[i]
if Dictionary[v] then
Dictionary = Dictionary[v]
else
return false, v .. " is not a valid child of " .. Dictionary.Name -- or some other property!
end
end
Dictionary[Path[#Path]] = Value
return true
end
I remembered from a while back that dictionaries were passed by reference and not value. Which is why I thought it’d work, though I must’ve forgotten that setting a variable = to a Non-dictionary Value in the dictionary would instead pass the value.