Extracting individual characters

Posted by Slayer on Wed 16 May 2007 02:05 AM — 12 posts, 46,197 views.

#0
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
Amended on Wed 16 May 2007 02:07 AM by Slayer
USA #1

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

Amended on Wed 16 May 2007 02:38 AM by Shaun Biggs
USA #2
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.
#3
Works perfectly. Thanks guys.
USA #4
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.
#5
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.
Amended on Wed 16 May 2007 03:35 AM by Slayer
#6
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
#7
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
Amended on Thu 17 May 2007 10:24 PM by Ayon
Australia Forum Administrator #8
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
#9
Thats absolute genius...it works perfectly.

0.0

Thank you very much on that...

/Ayon
USA #10
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?
Australia Forum Administrator #11
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