Issue with replaceAll() function

Post Reply
Knute
Posts: 87
Joined: Fri Mar 05, 2010 12:08 am

Issue with replaceAll() function

Post by Knute »

I'm not sure if I'm misunderstanding the manual on this command or what.
replaceAll( what, with )

replaces all occurrences of what in the current line with with → replace()
Ok, so my understanding of what this says is that I can do something like

Code: Select all

selectString(line, 1)
replaceAll(matches[1], matches[4] .. ": " .. matches[3] .. " (" .. matches[2] .. ")")
and it should replace every instance of the trigger line say (\d+) (\w+) (vials|vial).

Instead of getting: vials: turquoise (2) it does nothing.
I understand the special case of one, and know that. I'm just curious if I'm misunderstanding it, or if it has changed, or if there is some undocumented feature there. :D

Jor'Mox
Posts: 1154
Joined: Wed Apr 03, 2013 2:19 am

Re: Issue with replaceAll() function

Post by Jor'Mox »

The way you mention the triggering line, it looks more like you are talking about what an alias should do, rather than a trigger. What input are you expecting from the game and/or user to trigger this text to be displayed?

Knute
Posts: 87
Joined: Fri Mar 05, 2010 12:08 am

Re: Issue with replaceAll() function

Post by Knute »

Well inventory. "You are holding:" then vials, pipes, etc....

Also, hunting joules in Hallifax's Matrix. It would be nice to gag the big long descriptions for the joules, and just have the colors and such. I tried using replaceAll, but again, it didn't work. I ended up using 6 different triggers to get the output like I wanted.

Jor'Mox
Posts: 1154
Joined: Wed Apr 03, 2013 2:19 am

Re: Issue with replaceAll() function

Post by Jor'Mox »

Well, first, have you tried just using replace? The example you gave seems to only have one instance of the text you are looking to overwrite. Second, have you made sure that your trigger is firing on the lines you want it to fire on?

If not, you can do something simple like put echo("test") at the beginning if the trigger. Then you should see test printed at the end of every line it fires on.

Knute
Posts: 87
Joined: Fri Mar 05, 2010 12:08 am

Re: Issue with replaceAll() function

Post by Knute »

I have used replace. Replace works on the first instance.
You are holding:
Vials: turquoise (2), 10 glass test tube vials, 3 simple oaken pipes, a soot-
blackened tinderbox, 4 garnet vials, an energy cube, a bloodstone vial, 3 vials, a
monolith sigil.
This is the input. As you can see, it works on the first one only, I have match all checked.

In short, replace does not do the job. replaceAll() seems to be what's needed here. Why doesn't it work?

Jor'Mox
Posts: 1154
Joined: Wed Apr 03, 2013 2:19 am

Re: Issue with replaceAll() function

Post by Jor'Mox »

So, first I notice that several sets of vials won't be matched by the pattern you have. Second, replaceAll replaces every instance of a given bit of text with something else, you are trying to replace several different things.

What you might try doing, since you seem to have trouble getting your trigger firing on later items, is just using the line variable and working from there. Something like this:
Code: [show] | [select all] lua
local newline = line
string.gsub(newline,"(%d+) (%w+) (vials)","%3: %2 (%1)")
selectString(line,1)
replace(newline)
That said, it just came to me why what you have isn't working. The line variable holds the original line contents before any triggers run. So after your first trigger changes the first item, the line no longer matches.

So, you can probably just change your "selectString(line,1)" to "selectString(matches[1],1)" and it will probably work.

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: Issue with replaceAll() function

Post by Heiko »

1. In trigger contexts the best select function is usually selectCaptureGroup(), because this automatically handles replacements and insertions :)
2. replace() is a client function written in C++ while replaceAll() is a convenience function written in Lua and defined in mudlet-lua/lua/GUIUtils.lua that uses the client replace() function. replace() always replaces the *selection* and nothing else. 1. you select what you need to replace 2. call replace() to replace the selection.



--- Replaces all occurrences of what in the current line with <i>with</i>.
---
--- @usage This will replace all occurrences of John with the word Doe.
--- <pre>
--- replaceAll("John", "Doe")
---
--- -- also handles recursive matches:
--- replaceAll("you", "you and me")
--- </pre>
function replaceAll(word, what)
local startp, endp = 1, 1
while true do
startp, endp = getCurrentLine():find(word, endp+(#what-#word)+1)
if not startp then break end
selectSection(startp-1, endp-startp+1)
replace(what)
end
end

Post Reply