How to get the first value in a dictionary if its name is a number?

I have a dictionary that looks something like this:

local t = {
	[43957] = "a",
	[42498] = "b",
	[85387] = "c"
}

I was wondering on how I could get the very first value in this dictionary. I can’t do t[1] because the script would think I’d be referring to a value in the dictionary with the name “[1]” (which doesn’t exist) and it would error.

I also can’t do t[43957] because I’m using an API, and that number changes every time.

Thanks in advance.

Here’s the answer:

next time try to do some researching and googling before asking. only ask if you cannot find the answer :slight_smile:

That is not what I meant. In the example you provided, the names of the values in said dictionary are strings, I’m trying to find the first value in a dictionary where all the values are numbers.

And, upon testing with the code anyway, it errored.

No need to be so rude, I was only trying to help. May i ask what was the error?

You may use for key, value in pairs(dictionary) do

and on the first time the loop plays, break the for loop

use

print(next(t))

to get the first item.

1 Like

That’s what I basically told them but apparently they say it does not work.

Oh, my bad, but I don’t see the problem with next? Not sure why he would say it doesn’t work.

1 Like

Use a for loop

for i,v in pairs(Table) do
    if type(i) == "number" then
        -- code here
    end
    break -- use break because we only need index 1
end
3 Likes

Also pretty much what I told him, but he has not responded.

I tried it with a different table and it worked, it’s likely something wrong with the API. Thank you for your help

1 Like