Print last Value in Table

local trainSpawnsAndLines = {["9"] = {"Ostheim", "Vingst"}}

How can i print the last Value in Table (Array) Vingst?

I believe this is what you want:

print(trainSpawnsAndLines[#trainSpawnsAndLines])

Its saying nil


try to change the “9” into a number 9, but if it’s that important to keep it as a string, then here:

local newtable = {}

for i,v in pairs(trainSpawnsAndLines) do
    if tonumber(i) then
        newtable[tonumber(i)] = v
    end
end

print(newtable[#newtable])

That is also printing nil


that’s weird, try printing the table itself

local newtable = {}

for i,v in pairs(trainSpawnsAndLines) do
    if tonumber(i) then
        newtable[tonumber(i)] = v
    end
end

print(newtable)

It prints the Table with Numbers at the end

If your trying to find the last item in 9 use this, but if your saying the last table within trainSpawnsAndLines then I’ll come up with something else.

print(trainSpawnsAndLines["9"][#trainSpawnsAndLines["9"]])
2 Likes
local trainSpawnsAndLines = {["9"] = {"Ostheim", "Vingst"}}
local lastDictionary = trainSpawnsAndLines[#trainSpawnsAndLines]
local lastValue = lastDictionary[#lastDictionary]
print(lastValue)-- should print "Vingst"
1 Like

And do you know if i have the “Ostheim” how i could go to Vingst like doing 1+

print(trainSpawnsAndLines["9"][1+1]])
1 Like

Thanks worked when i removed one of your ]

I have one last question how could i use this instead 1?:

print(trainSpawnsAndLines["9"]["Ostheim"+1]) or print(trainSpawnsAndLines["9"]["Vingst"+1])

Unfortunately that wouldn’t be possible.

Would it not work with table.find?

Ah if your trying to find a value from a table without any complicated methods
then yes you could use table.find.

If i table find Vingst how could i go to the next?

If you use table.find it means you want to find one value from the table, there’s no other way to “go next” after that. If you want to go to the next value then just use this

That wouldn’t work, the length of dictionaries is 0.

1 Like