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.
 Entire forum ➜ MUSHclient ➜ Lua ➜ Multiline Capture Trigger and Lua Tables

Multiline Capture Trigger and Lua Tables

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


Posted by Strazor   USA  (24 posts)  Bio
Date Sun 03 Jan 2016 12:11 AM (UTC)

Amended on Sun 03 Jan 2016 12:25 AM (UTC) by Strazor

Message
After reading the Multiline Trigger Capture FAQ (http://mushclient.com/forum/?id=7794#37) I've had success capturing the data I need into a LUA table. Now I'm looking for a way to strip that data down and only capture one column of information.

Here is the LUA Table Output


  1 red        Dread Guardian       201 Warrior    Giant        12 May 2013
  2 green    Dread Guardian       201 Warrior   Vampire    02 Jan 2016
  3 blue      Eclipse Lord             201 Cleric      Human      08 Dec 2015
  4 purple   Haze Succubus       201 Mage       Human      25 Sep 2014 


What I want to do is extract the names of the colors and put them into another table (I don't need the other data from the multiline capture).

The output I'm wanting would look like this


red
green
blue
purple


Any thoughts on how to parse the data this way?
Top

Posted by Nick Gammon   Australia  (23,094 posts)  Bio   Forum Administrator
Date Reply #1 on Sun 03 Jan 2016 03:32 AM (UTC)

Amended on Sun 03 Jan 2016 03:34 AM (UTC) by Nick Gammon

Message
Assuming your table data looks exactly like that (which I reproduce in my test) then a simple string.match will do it:


tbl = {
  "red        Dread Guardian       201 Warrior    Giant        12 May 2013",
  "green    Dread Guardian       201 Warrior   Vampire    02 Jan 2016",
  "blue      Eclipse Lord             201 Cleric      Human      08 Dec 2015",
  "purple   Haze Succubus       201 Mage       Human      25 Sep 2014 "
}

for k, v in pairs (tbl) do   -- k = key, v = value
  colour = string.match (v, "^%a+")
  if colour then
    print (colour)
  end -- if
end -- for


Output:


red
green
blue
purple


Documentation for Lua string matching:

http://www.gammon.com.au/scripts/doc.php?lua=string.find

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Strazor   USA  (24 posts)  Bio
Date Reply #2 on Sun 03 Jan 2016 03:45 AM (UTC)

Amended on Sun 03 Jan 2016 04:02 AM (UTC) by Strazor

Message
Thanks Nick. My issue now is that the data I listed does not have a key value. The numbers (1,2,3,4) are from the Multiline Trigger I captured. is it possible to create the table with Keys before I capture the data or should I just copy the data to create a new table?
Top

Posted by Fiendish   USA  (2,533 posts)  Bio   Global Moderator
Date Reply #3 on Sun 03 Jan 2016 11:54 AM (UTC)

Amended on Sun 03 Jan 2016 12:15 PM (UTC) by Fiendish

Message
Nick already doesn't actually use the table key for anything. The pairs iterator will work for a non-keyed table too so you don't have to do anything and can just use Nick's example directly. If it makes you feel better about the code, you can change "pairs" to "ipairs". It will most likely do exactly the same* thing for your case, but will be semantically more correct.

This peers into one of the more interesting aspects of Lua language, which is that tables are a melange of both key-value dictionaries and ordered-value lists, and you can mix the two into the same table, because really all non-keyed table entries are just automatically given numeric keys.

Try doing each of the following in a Lua script and see how the results differ:


-- using pairs

> for k,v in pairs({"hello", "goodbye"}) do print(k,v) end
1	hello
2	goodbye

> for k,v in pairs({["3"]="foo", "hello", "goodbye"}) do print(k,v) end
1	hello
2	goodbye
3	foo

-- 1 and "1" are different types, so both get put into the table
> for k,v in pairs({["1"]="foo", "hello", "goodbye"}) do print(k,v) end
1	hello
2	goodbye
1	foo

-- using ipairs

-- "1" is ignored, because it's not a number and ipairs only does numbered entries
> for k,v in ipairs({["1"]="foo", "hello", "goodbye"}) do print(k,v) end
1	hello
2	goodbye

> for k,v in ipairs({[3]="foo", "hello", "goodbye"}) do print(k,v) end
1	hello
2	goodbye
3	foo

-- 1="foo" gets overruled by "hello" being the first non-keyed value, because the latter takes priority
> for k,v in ipairs({[1]="foo", "hello", "goodbye"}) do print(k,v) end
1	hello
2	goodbye

-- ipairs stops at the first gap in numbers
-- since there is no 3, it never sees 4
> for k,v in ipairs({[4]="foo", "hello", "goodbye"}) do print(k,v) end
1	hello
2	goodbye


* - I think there may be some small risk that using pairs instead of ipairs on a non-keyed table could potentially return the results out of order, though? Not sure how real that memory is.

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

Posted by Nick Gammon   Australia  (23,094 posts)  Bio   Forum Administrator
Date Reply #4 on Sun 03 Jan 2016 08:48 PM (UTC)
Message
Quote:

I think there may be some small risk that using pairs instead of ipairs on a non-keyed table could potentially return the results out of order, though?


Correct. I meant to use ipairs, but pairs will work. I believe that Lua tables actually have a numeric and non-numeric part, so that pairs would (in most cases) return keys in sequence. It switches to the non-numeric part for non-numbers (obviously) and also if there is a large gap in the number sequence.

Quote:

My issue now is that the data I listed does not have a key value.


You can generate keys by doing a table.insert for each line, which will just generate keys starting at 1.

http://www.gammon.com.au/scripts/doc.php?lua=table.insert

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


17,133 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.