'LinKin Posted March 16, 2014 Share Posted March 16, 2014 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 comment
pa3ck Posted March 16, 2014 Share Posted March 16, 2014 Yea, it will work just fine. If myTable[row-1] returns nil or false, it will automatically return myTable[9]. Link to comment
madis Posted March 16, 2014 Share Posted March 16, 2014 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 comment
Moderators IIYAMA Posted March 17, 2014 Moderators Share Posted March 17, 2014 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 comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now