WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Reverse string (https://www.wowinterface.com/forums/showthread.php?t=59005)

abel8909 12-25-21 03:36 PM

Reverse string
 
hi everyone and happy christmas eve, i'm new to this lua, could someone help me how to reverse string example
FACTION_STANDING_INCREASED = "%s %d"
for
FACTION_STANDING_INCREASED = "%d %s"

I found this code but it doesn't work

Quote:

ChatFrame_AddMessageEventFilter("CHAT_MSG_COMBAT_FACTION_CHANGE", function(frame, event, message, ...)
local which, faction, amount = strmatch(message, "^[%+%-] (%+) (%d+) rep$")
if which and faction and amount then
return false, (format("%s%d %s rep", which, amount, faction)), ...
end
end)

SDPhantom 12-25-21 03:44 PM

Specific to the WoW API, string.format() lets you specify which args go where by modifying the format specifier. By default, they are left-to-right in order. This would flip the order by specifying the second argument goes in the first slot and vice versa. Blizzard did this specifically to support localization.
Code:

FACTION_STANDING_INCREASED = "%2$d %1$s"
Note: The letters define the data type, changing these to be different than what the arguments are can cause type mismatch errors.

abel8909 12-25-21 07:50 PM

Quote:

Originally Posted by SDPhantom (Post 340124)
Specific to the WoW API, string.format() lets you specify which args go where by modifying the format specifier. By default, they are left-to-right in order. This would flip the order by specifying the second argument goes in the first slot and vice versa. Blizzard did this specifically to support localization.
Code:

FACTION_STANDING_INCREASED = "%2$d %1$s"
Note: The letters define the data type, changing these to be different than what the arguments are can cause type mismatch errors.

if it works, thank you very much, and another question if it does not bother you :P
how can I filter chat texts (from battleground) to replace them example

Babe claims the stables! If left unchallenged, the Alliance will control it in 1 minute!

for

Babe claims the stables!

this is the event

Kanegasi 12-25-21 09:12 PM

Lua Code:
  1. ChatFrame_AddMessageEventFilter("CHAT_MSG_BG_SYSTEM_ALLIANCE",function(_,_,msg,...)
  2.     msg=msg:gsub(" If left unchallenged, the Alliance will control it in 1 minute!","")
  3.     return nil,msg,...
  4. end)

abel8909 12-26-21 05:25 PM

Quote:

Originally Posted by Kanegasi (Post 340126)
Lua Code:
  1. ChatFrame_AddMessageEventFilter("CHAT_MSG_BG_SYSTEM_ALLIANCE",function(_,_,msg,...)
  2.     msg=msg:gsub(" If left unchallenged, the Alliance will control it in 1 minute!","")
  3.     return nil,msg,...
  4. end)

Thank you very much for answering, if it works and excuse my ignorance, how can I make a list to filter several messages? I did something like that but it doesn't work

Lua Code:
  1. ChatFrame_AddMessageEventFilter("CHAT_MSG_BG_SYSTEM_ALLIANCE",function(_,_,msg,...)
  2.     msg=msg:gsub(" If left unchallenged, the Alliance will control it in 1 minute!"," control it in 1 minute!")
  3.     msg=msg:gsub("The Horde Flag was picked up by ","The Horde Flag was captured by ")
  4.     return nil,msg,...
  5. end)

Kanegasi 12-26-21 07:30 PM

The gsub function accepts three arguments: the string, what to look for, and what to substitute.

msg=gsub(msg,find,replace)

or

msg=msg:gsub(find,replace)

This means that replace being an empty string, "", you are deleting what the function finds.

Lua Code:
  1. ChatFrame_AddMessageEventFilter("CHAT_MSG_BG_SYSTEM_ALLIANCE",function(_,_,msg,...)
  2.     msg=msg:gsub(" If left unchallenged, the Alliance will control it in 1 minute!","")
  3.     msg=msg:gsub(" control it in 1 minute!","")
  4.     return nil,msg,...
  5. end)

The above code only filters the blue text. To alter the red text, you need to filter the Horde event.

Lua Code:
  1. ChatFrame_AddMessageEventFilter("CHAT_MSG_BG_SYSTEM_HORDE",function(_,_,msg,...)
  2.     msg=msg:gsub("The Horde Flag was picked up by ","")
  3.     msg=msg:gsub("The Horde Flag was captured by ","")
  4.     return nil,msg,...
  5. end)

If I got the colors wrong, I apologize. The messages above may all be blue, it's been a long time since I played battlegrounds.

Edit:

Now that I think about it, you don't have to worry about the different events if you just filter all of them at once.

Lua Code:
  1. local filterfunc=function(_,_,msg,...)
  2.     msg=msg:gsub(" If left unchallenged, the Alliance will control it in 1 minute!","")
  3.     msg=msg:gsub(" control it in 1 minute!","")
  4.     msg=msg:gsub("The Horde Flag was picked up by ","")
  5.     msg=msg:gsub("The Horde Flag was captured by ","")
  6.     return nil,msg,...
  7. end
  8.  
  9. ChatFrame_AddMessageEventFilter("CHAT_MSG_BG_SYSTEM_ALLIANCE",filterfunc)
  10. ChatFrame_AddMessageEventFilter("CHAT_MSG_BG_SYSTEM_HORDE",filterfunc)
  11. ChatFrame_AddMessageEventFilter("CHAT_MSG_BG_SYSTEM_NEUTRAL",filterfunc)

This takes one filter function and applies it to all three BG announcement events.

abel8909 12-26-21 08:25 PM

oh i see, thank you very much


All times are GMT -6. The time now is 07:19 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI