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

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Extracting individual characters
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Extracting individual characters

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


Posted by Slayer   (4 posts)  [Biography] bio
Date Wed 16 May 2007 02:05 AM (UTC)

Amended on Wed 16 May 2007 02:07 AM (UTC) by Slayer

Message
Taken from a previous thread, lost my username... oh well.

I want to extract individual characters from a line that have different ansi to the rest of the line.
The script below separates and displays the whole line;

function my_trigger (name, line, wildcards, styles)

for k, v in ipairs (styles) do
print ("TEST: ", v.text)
end -- for loop

end -- my_trigger

ie, atm if I triggered of a line, where (x) indicates x is a different colour, such as: This i(s) a tes(t) li(n)e.

It would give me
TEST: This i
TEST: s
TEST: a tes
TEST: t
TEST: li
TEST: n
TEST: e.

Anyway to make it ignore certain colours, or ignore anything that has more than 1 character?
Oh and also to have it print the extracted chars on the same line? So the above example would give me:
TEST: stn
Cheers,
DS
[Go to top] top

Posted by Shaun Biggs   USA  (644 posts)  [Biography] bio
Date Reply #1 on Wed 16 May 2007 02:20 AM (UTC)

Amended on Wed 16 May 2007 02:38 AM (UTC) by Shaun Biggs

Message

ignore = { ["red"] = false, ["blue"] = false }
function my_trigger (name, line, wildcards, styles)
  local temp = "TEST: "
  for k, v in ipairs (styles) do
    if string.len(v.text) == 1 and ( ignore[ RGBColourToName( v.textcolour ) ] or true ) then
      temp = temp..v.text
    end
  end -- for loop
  print( temp )
end -- my_trigger


It is much easier to fight for one's ideals than to live up to them.
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] bio
Date Reply #2 on Wed 16 May 2007 02:43 AM (UTC)
Message
ignore[ RGBColourToName( v.textcolour ) ] or true

that disjunction will always be true. You probably want to do

not ignore[ RGBColourToName( v.textcolour ) ]

and then set ignore[color] = true when color is something you want to ignore.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
[Go to top] top

Posted by Slayer   (4 posts)  [Biography] bio
Date Reply #3 on Wed 16 May 2007 02:59 AM (UTC)
Message
Works perfectly. Thanks guys.
[Go to top] top

Posted by Shaun Biggs   USA  (644 posts)  [Biography] bio
Date Reply #4 on Wed 16 May 2007 03:05 AM (UTC)
Message
Yeah, wasn't thinking about the false or true being true. And I keep forgetting that not nil is true. In my brain I was thinking of having ignore[ RGBColourToName( v.textcolour ) ] ~= false, but used the or out of habit.

It is much easier to fight for one's ideals than to live up to them.
[Go to top] top

Posted by Slayer   (4 posts)  [Biography] bio
Date Reply #5 on Wed 16 May 2007 03:34 AM (UTC)

Amended on Wed 16 May 2007 03:35 AM (UTC) by Slayer

Message
Well I cant get the ignore colour to work, but turns out I don't really need it. There is never more than 1 differently coloured character in a row.
[Go to top] top

Posted by Slayer   (4 posts)  [Biography] bio
Date Reply #6 on Wed 16 May 2007 06:16 AM (UTC)
Message
Heh colours werent working because I didn't know the 'real' names of the colours... olive/maroon/navy... looks like brown/red/blue to me :P
[Go to top] top

Posted by Ayon   (6 posts)  [Biography] bio
Date Reply #7 on Thu 17 May 2007 10:19 PM (UTC)

Amended on Thu 17 May 2007 10:24 PM (UTC) by Ayon

Message
Hey All

I just switched back to lua, and I am working on a thinger that needs to recognize the color of a single character in a string, but I also need to know the index, that is, where is it located in the string.

say for instance, I will have a string like this:
123456789...etc
'..*. * . < % * .. . .'
Now, let's say, that last asterisk is red, all the rest are grey or white. Now, I need to know which asterisk is red, and what the number position is. This is so I can figure out my place on a map, each character means a certain thing, but only one character means me, who I am.
Code:

function maptracker (sName,sLine,wildcards,styles) 
    line = styles[2] -- The matched text will be here.
    if (string.find(line.text,'*')) then
      --start pseudo code
       i = 1
      for each char in line.text do
        if char == red asterisk then
          my_map_column_position = i
        else
          i = i + 1
        end
      end 
     --end pseudo code
    end
 end
end -- maptracker

So, my main question, is how do I look at each char in the string, and the color of that char, and keep the place number?

Thanks for any help you can give me on this :)

/Ayon
[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] bio   Forum Administrator
Date Reply #8 on Fri 18 May 2007 12:54 AM (UTC)
Message
See this post:

http://mushclient.com/forum/?id=7818

That shows how you can find the colour of a character at a particular column.

Given that, you could do something like this:


for i = 1, #sLine do  -- look at each character
  if string.sub (sLine, i, i) == "*" then  -- if asterisk
    t = GetStyle (styles, i) -- get style table
    if RGBColourToName (t.textcolour) == "red" then
       -- blah blah - do something
    end -- if red
  end -- if asterisk
end -- for loop

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Ayon   (6 posts)  [Biography] bio
Date Reply #9 on Fri 18 May 2007 01:07 AM (UTC)
Message
Thats absolute genius...it works perfectly.

0.0

Thank you very much on that...

/Ayon
[Go to top] top

Posted by Shaun Biggs   USA  (644 posts)  [Biography] bio
Date Reply #10 on Tue 22 May 2007 12:03 AM (UTC)
Message
As kind of a side question, is there any way to quickly get the style of a wildcard, or should I just rematch the pattern to get the starting and ending positions in the trigger string and copy it over from the style table?

It is much easier to fight for one's ideals than to live up to them.
[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] bio   Forum Administrator
Date Reply #11 on Tue 22 May 2007 02:20 AM (UTC)
Message
If the pattern was simple, you might just use string.find to find the offsets.

Otherwise, rematch using re:exec from the Lua rex library, which returns the offsets of each wildcard, and then you can use those to find the styles.

http://www.gammon.com.au/scripts/doc.php?lua=re:exec

- Nick Gammon

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


5,618 views.

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.

[Home]


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 FutureQuest]