Jump to content

Is this ok?


'LinKin

Recommended Posts

Hello,

Is this ok?

local myVariable = myTable[row-1] or myTable[9]

I want to return a string according to a number. I have these strings saved in a table. Which lengh is 9.

So for example if the row is 11, it will try to look for the string stored in myTable[10] but it would be nil. So I want it to return the 9th String if the row > 9.

Thanks.

Link to post

It works, but with the current code value is myTable[9] also if row=1 or value of array element is false or nil.

You could also do this:

local myVariable = myTable[math.min(row-1, 9)]

in which case if row is less than 2, myVariable would be nil and also if element value is false or nil, it will be propagated to myVariable as well (not take the 9th element).

Link to post
Hello,

Is this ok?

local myVariable = myTable[row-1] or myTable[9]

I want to return a string according to a number. I have these strings saved in a table. Which lengh is 9.

So for example if the row is 11, it will try to look for the string stored in myTable[10] but it would be nil. So I want it to return the 9th String if the row > 9.

Thanks.

This will count down from the highest array, and check if they are nil/false and count one down till it's found it's target.

local myVariable 
for i=#myTable,1,-1 do -- counts down till 1. 
    local variable = myTable[i] 
    if variable then 
        myVariable = variable 
        break 
    end 
end 

If you use they array I will recommend you to use table.remove, so you won't have trouble with empty places.

Link to post

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...