[Home] [Downloads] [Search] [Help/forum]

Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  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] Refresh page

Posted by Gpan   (4 posts)  [Biography] bio
Date Tue 12 Jan 2021 05:10 AM (UTC)
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
[Go to top] top

Posted by Fiendish   USA  (2,525 posts)  [Biography] 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

Set X and Y like this:

X = "apple"
Y = {

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 = {

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

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

[Go to top] 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.


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

Go to topic:           Search the forum

[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

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


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]