Jump to content

[help] Find position of the specific value in table


monday

Recommended Posts

Posted

Is there any easy way to find it? Let's say that I have a table like this:

mytable = {999, 1001, 996, 991} 

And I'd like to find the position of 996 by something like:

number = tableMagicFind(mytable, 996) 
--number would be 3 

  • MTA Team
Posted
for index, value in pairs(mytable) do 
    if (value == 996) then 
        return index 
    end 
 end 

You can move that to a function and replace mytable and 996 with function parameters.

Posted

You can also do it like this;

local selectedIndex = mytable[3]; 
-- Will return 996 

It works similarily if your table has another table inside of it. IE;

local myTable = {{1, 2, 3}, {"A", "B", "C"}, {1, 2, 3}}; 
local selectedIndex = myTable[2][3]; 
-- Will return C 

Edit:

Nevermind, just saw that you wanted to find the position of it :P

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...