Capturing and trimming a who list.

Posted by Wuggly on Wed 13 Jul 2016 01:37 PM — 11 posts, 38,026 views.

USA #0
Whenever the who list is checked, the styles are recorded into a whoL table.

What I am trying to do, is right before it writes it into the mini-window, check the style.text to see what format it is in.

Whether it's like this:

Hum [ Ra:30 Th:30 Cl:30 ] Wuggly, the noob scripter.

or like this:

Elf [    God    ] Nick, the grand master.

This way I can trim off the comma and everything after so it just shows it like:

Hum [ Wa:30 Th:30 Ma:30 ] Wuggly

I'm just unsure how to go about this. Maybe something like string.find to detect the comma then gsub to cut it out?

The part I'm really confused about is when it doesn't show the class/classes and is just a word, like Demigod, God, etc.. due to those extra spaces. Then you've got two worded ones like Lesser God and Adept Finder.

Here's the code for when it writes it into the mini-winder, nothing fancy.

 if whoL ~= nil then
   local y = font_height * 2 + 35
   for i, styles in ipairs (whoL) do
     local x = 5
     for _, style in ipairs (styles) do
      x = x + WindowText (status_win, font, style.text, x, y, 0, 0, style.textcolour)
     end -- for
     y = y + font_height
   end -- for 
  end -- whoL


What's a good way to go about this?
Amended on Wed 13 Jul 2016 04:57 PM by Wuggly
Australia Forum Administrator #1
Isn't this like your other question?

http://www.gammon.com.au/forum/?id=13708

Now you want to capture up to a character (the comma) rather than after it?
Amended on Wed 13 Jul 2016 08:18 PM by Nick Gammon
USA #2
I've tried reversing it, but just can't figure it out.

First attempt

foo = string.match (test, "(.*),.-")


Second attempt

foo = string.match (test, "(.*)%,.-")


EDIT: Finally did what I should of done a long time ago rather than bugging all of you, and researched.

...and got it to work!

foo = string.match (test, "^(.*),.-$")


Found out the .- was matching nothing until I specified the end of the line.

In case anyone is wondering where I found the info about the .-, it was here:
http://lua-users.org/wiki/PatternsTutorial

Couldn't find anything about the .- in the MUSHclient documentation.
http://www.gammon.com.au/scripts/doc.php?general=regexp
and
http://www.gammon.com.au/regexp
Amended on Fri 15 Jul 2016 03:52 PM by Wuggly
Australia Forum Administrator #3
Since this is a Lua pattern, not a PCRE regexp, you need to look at the documentation for string.find:

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

The dot matches anything, and the "-" qualifier is "zero or more, non-greedy".

Without the "$" at the end the least greedy match is nothing at all.

You can see the difference here:


print (string.find ("#hello# #world#", "(#.*#)")) --> 1 15 #hello# #world# 

print (string.find ("#hello# #world#", "(#.-#)")) --> 1 7 #hello# 


The first one uses a greedy match, so to find the closing "#" symbol it consumes everything in the string (including other "#" symbols) which is greedy.

The second one uses a non-greedy match, so it stops as soon as it can, which is the first "#" symbol.
Amended on Sat 16 Jul 2016 09:30 AM by Nick Gammon
USA #4
Thanks for the information.

I'm down to one last thing I can't work out and that's for names that have no commas or periods to detect right after the name.

IE:

Hum [ Ra:30 Th:30 Cl:30 ] Wuggly explores the realm.


Here's the two things I've tried.

whoT = string.match (style.text, "^(.*) %[ (.*) %] (.*?) .-$")

and the other way I simple replaced the spaces with %s
also tried without the ?

sorry im so slow when it come to regexp :/
Amended on Sat 16 Jul 2016 08:07 PM by Wuggly
Australia Forum Administrator #5
Is the name one word?
USA #6
Yes, on the server I play, names are one word.
Australia Forum Administrator #7
Well, instead of searching for the comma, which may not be there anyway, search for one word.

For example: %a+

That would match one or more letters, and stop at the first space (or comma, or whatever).
USA #8
I've been trying, but having no luck.

This is the closest I've gotten so far to getting it.

BEFORE:
3 characters.  Total online: 5.
Ogr [ Sh: 2 Wa:30 Tg:25 ] Blake. Meat Shield
Elf [    Ra:22 Ma:24    ] Cusithe growls as she Hunts. [ShroomMinion]
Elf [ Ma:30 Wa:30 Th:30 ] Nicholai- master of lost avatars


PATTERN USED:
whoT = string.match (style.text, "^(.*) %a+ .-$")


AFTER:
3 characters. 
Ogr [ Sh: 2 Wa:30 Tg:25 ] Blake.
Elf [    Ra:22 Ma:24    ] Cusithe growls as
Elf [ Ma:30 Wa:30 Th:30 ] Nicholai- master of


Every time I try to mess with the brackets, it always gets worse. Here's one example.

BEFORE:
6 characters.  Total online: 6.
Ogr [ Sh: 2 Wa:30 Tg:25 ] Blake. Meat Shield
Hum [    Cl:28 Wa:28    ] Noah needs a shave
Elf [    Ra:22 Ma:24    ] Cusithe growls as she Hunts. [ShroomMinion]
Ogr [ Sh:26 Tg:23 Wa:26 ] Uzzi, I'm too sexy for my shirt!
Hum [ Wa:30 Tg:30 Cl:30 ] Vex Ironwolf, the Grim Warder
Elf [ Ma:30 Wa:30 Th:30 ] Nicholai- master of lost avatars


PATTERN USED:
whoT = string.match (style.text, "^(.*) %] %a+ .-$")


AFTER:
nil
nil
Hum [    Cl:28 Wa:28   
Elf [    Ra:22 Ma:24   
nil
Hum [ Wa:30 Tg:30 Cl:30
nil
Amended on Tue 19 Jul 2016 06:59 PM by Wuggly
Australia Forum Administrator #9
How about this?


  whoT = string.match (line, "^(.- %] %a+)")


That matches anything, non-greedily, until it reaches a space followed by a square bracket and another space. Then it stops after the next word. Output:


Ogr [ Sh: 2 Wa:30 Tg:25 ] Blake
Hum [    Cl:28 Wa:28    ] Noah
Elf [    Ra:22 Ma:24    ] Cusithe
Ogr [ Sh:26 Tg:23 Wa:26 ] Uzzi
Hum [ Wa:30 Tg:30 Cl:30 ] Vex
Elf [ Ma:30 Wa:30 Th:30 ] Nicholai
Amended on Tue 19 Jul 2016 08:11 PM by Nick Gammon
USA #10
Worked perfect, and I think I finally understand expressions a lot better now.

For example, just playing around, I found I could display it however I want it..


whoT = string.match (style.text, "^(.- %] %a+)")

-- for race
whoRa = string.match(whoT, "^(%a+ .-)")
-- for class/classes
whoCl = string.match(whoT, "^.-(%[.*%])")
-- for name
whoNa = string.match(whoT, "( %a+)$")


Huge thanks Nick!