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, confirm your email, resolve issues, 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.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ General ➜ Matching a word from a lua table string

Matching a word from a lua table string

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


Posted by Strazor   USA  (24 posts)  Bio
Date Mon 01 Sep 2014 09:53 PM (UTC)
Message
In a previous post here: http://www.gammon.com.au/forum/bbshowpost.php?bbsubject_id=12569 Nick showed me how to sort my table by adding keys or extract information. What I am trying to do now is match a particular word from a string of words in a table. Here is my current table.


equipinform = {

["468263929"] = {
    level=1,
    weight=1,
    wearloc="sleeping",
    details_captured=true,
    stats = {},
    owner="Strazor",
    objectid="12569898",
    flags="glow, hum, magic, held, nopurge, burn-proof, nolocate, solidified, resonated, illuminated, v3, precious",
    itemtype="Furniture",
    container_id="worn",
    resists = {},
    name="V3 Trivia Sleeping Bag",
    score="0",
    },

["965742031"] = {
    level=41,
    weight=0,
    wearloc="neck",
    details_captured=true,
    stats = {
      damageroll=29,
      luck=2,
      moves=-30,
      wisdom=2,
      intelligence=2,
      hitroll=1,
      hitpoints=25,
      },
    owner="",
    objectid="125689755",
    flags="unique, glow, hum, magic, held, burn-proof, solidified, illuminated, v3, searchable",
    itemtype="Armor",
    container_id="worn",
    resists = {},
    name="(>Asura's Azurite Pearl<)",
    score="205",
    }

} -- end of equipinform

tprint (equipinform)


Looking at the above table "flags" contains a lot of information. I would like to match the word "precious" so the script prints only the item(s) that have this keyword. For now a simple, true/false would work. Does lua provide a statement that allows this sort of data matching?
Top

Posted by Strazor   USA  (24 posts)  Bio
Date Reply #1 on Tue 02 Sep 2014 02:32 AM (UTC)

Amended on Tue 02 Sep 2014 09:25 AM (UTC) by Strazor

Message
Well, as most things go. Ask a question and the answer usually presents itself.

I realized that I can set a variable to match the string I would like.


var = item.flags

if string.find (flagtype, "precious") then
     ColourNote ("red", "black", "FLAG FOUND")
else
     ColourNote ("red", "black", "FLAG NOT FOUND")
end

print(item.flags)  -- verify if statement works correctly


edit -- changed the above code to fix the error Nick discusses below with print().
Top

Posted by Nick Gammon   Australia  (23,173 posts)  Bio   Forum Administrator
Date Reply #2 on Tue 02 Sep 2014 06:10 AM (UTC)
Message

print("item.flags")  -- verify if statement works correctly


Should be:


print(item.flags)  -- verify if statement works correctly


... shouldn't it?

But yes, doing a string.find or a string.match should let you search for a particular flag.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
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.


15,012 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.