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
➜ Lua
➜ Extracting individual characters
Extracting individual characters
|
It is now over 60 days since the last post. This thread is closed.
Refresh page
Posted by
| Slayer
(4 posts) 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 | Top |
|
Posted by
| Shaun Biggs
USA (644 posts) 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. | Top |
|
Posted by
| David Haley
USA (3,881 posts) 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 | Top |
|
Posted by
| Slayer
(4 posts) Bio
|
Date
| Reply #3 on Wed 16 May 2007 02:59 AM (UTC) |
Message
| Works perfectly. Thanks guys. | Top |
|
Posted by
| Shaun Biggs
USA (644 posts) 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. | Top |
|
Posted by
| Slayer
(4 posts) 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. | Top |
|
Posted by
| Slayer
(4 posts) 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 | Top |
|
Posted by
| Ayon
(6 posts) 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 | Top |
|
Posted by
| Nick Gammon
Australia (23,158 posts) 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 | Top |
|
Posted by
| Ayon
(6 posts) 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 | Top |
|
Posted by
| Shaun Biggs
USA (644 posts) 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. | Top |
|
Posted by
| Nick Gammon
Australia (23,158 posts) 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 | 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.
35,348 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top