Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to "verify" your details, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.
 Entire forum ➜ MUSHclient ➜ Lua ➜ Determine whether x belongs to y array

Determine whether x belongs to y array

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Gpan   (4 posts)  Bio
Date Tue 12 Jan 2021 05:10 AM (UTC)
Message
x = apple Y = {apple|pear} Z={sword|blade}

if X belongs to Y, then return Y

one function like this: if Y[X] then return Y.

i wanna know is there a packaged func can do the same thing
Top

Posted by Fiendish   USA  (2,533 posts)  Bio   Global Moderator
Date Reply #1 on Tue 12 Jan 2021 08:02 AM (UTC)

Amended on Tue 12 Jan 2021 08:07 AM (UTC) by Fiendish

Message
Set X and Y like this:

X = "apple"
Y = {
 ["apple"]=true,
 ["pear"]=true,
}


Then Y[X] will be true if X is a key in Y and nil if not.

Then you can use something like
(Y[X] and Y)


That will equal Y if X is in Y, otherwise it will equal nil.

Or if you want it in function form, what you had is almost complete:


X = "apple"
Y = {
 ["apple"]=true,
 ["pear"]=true,
}

function a_if_b_in_a(a, b)
   if a[b] then return a end
end

result = a_if_b_in_a(Y, X)
same_result = (Y[X] and Y)

https://github.com/fiendish/aardwolfclientpackage
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


8,220 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.