How to Print Dictionaries?

local dictionaries = {

“1234” = {
num1
}

“4321” = {
num2

}

}

How could i print the num2 and make a if statement to num2

1 Like

That’d be dictionaries["4321"][1]

You can print it or make an argument to it.

print(dictionaries["4321"][1])

if dictionaries["4321"][1] then
3 Likes

Okay thanks how could i do the if statement? Like: If num2 == 10 then
end

I dont saw the other message thanks for ur help

And how could i use table.find for num1? I mean if they dont find num1 in the table “then”

Use if not preferences,

For example:

if not table.find(table, "num1") then
	print("Found nothing")
end
2 Likes

This will only work if num1 is a string "num1’ and if the table is an array (dictionaries["1234"] does happen to be an array.)

table.find is used in arrays instead of dictionaries because it parses through the array in order, then stops at the first matching value, not key. It’s not possible to parse through a dictionary sequentially since it contains keys instead of integers.

If OP were to use table.find(dictionaries["1234"], "num1") it would return nil because the array doesn’t contain a “num1” string.

OP would need to search by the variable itself. For example:

table.find(dictionaries["1234"], num1)

But again to emphasize, that only works with arrays not dictionaries.

1 Like

Hey i have one another Question, could i also search for num2 in “1234”, “4321” “0102” and more…
To search num6 in all arrays.

Yep. You can use the table.find on any specific array you want or you can run a loop to search the whole dictionary.

This example goes through every array ("1234", "4321", etc.) in the first layer of your dictionary (dictionaries).

for ArrayName, Array in pairs (dictionaries) do --This passes through ALL items in the dictionary. ArrayName is set to represent the key and Array is set to represent the value.
	if typeof(Array) == "table" --This makes sure the Array variable is a table before using a table function on it.
		if table.find(Array, num2) then --Searches the array for any value that matches the variable num2.
			print( 'The array "' ..tostring(ArrayName) .. '" contains a value matching ' ..tostring(num2) .. '.' )
		end
	end
end

Do i need the: if typeof(Array) == “table” then

Because i will just insert there tables

and could i do also something like this?:

for a, Array in pairs (Matches) do

			if table.find(Matches, player.UserId) then

print(“Found 1”)
end
if typeof(Array) == “table”
if table.find(Array, player.UserId) then
print(“Found 2”)

				end
			end
		end

Its called keywords, not preferences.

I know now how to do it, Thanks you for your help!

I tried it and its not working:

for a, playerInTable in pairs (Matches) do
local checkIfPlayerIsInTable = Matches[tostring(player.UserId)]
if checkIfPlayerIsInTable then

	Matches[tostring(player.UserId)] = nil

end

if table.find(playerInTable, tostring(player.UserId)) then

	print("1")

	print(playerInTable, tostring(player.UserId))

	table.remove(playerInTable, tostring(player.UserId))

	print(playerInTable, tostring(player.UserId))					

end

end