AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Replacing a part of a message (https://forums.alliedmods.net/showthread.php?t=144384)

bibu 12-03-2010 02:50

Replacing a part of a message
 
I am using this in my code:

PHP Code:

client_print(idprint_chat"bibu said %s to all"g_szWelcomeMsgs[g_RandomMsg]) 

I also use the welcome msg in a menu, so it does display like this:

bibu said \yhi

Now in the menu it looks colored, but I want that if \y or \w or \r, \d contained, removed in the client_print part.
Can someone help me with that?

Exolent[jNr] 12-03-2010 02:56

Re: Replacing a part of a message
 
Code:
new szMessage[ sizeof( g_szWelcomeMsgs[ ] ) ]; copy( szMessage, charsmax( szMessage ), g_szWelcomeMsgs[ g_RandomMsg ] ); new const szMenuCodes[ ][ ] = { "\r", "\y", "\w", "\d", "\R" }; for( new i = 0; i < sizeof( szMenuCodes ); i++ ) {     replace_all( szMessage, charsmax( szMessage ), szMenuCodes[ i ], "" ); } client_print( id, print_chat, "bibu said %s to all", szMessage );

Seta00 12-03-2010 03:40

Re: Replacing a part of a message
 
here'sapostwithnowhitespacetocompensatethe previouspost

Exolent[jNr] 12-03-2010 04:01

Re: Replacing a part of a message
 
Quote:

Originally Posted by Seta00 (Post 1361912)
here'sapostwithnowhitespacetocompensatethe previouspost

Sorry.

Code:
new szMessage[sizeof(g_szWelcomeMsgs[])]; copy(szMessage,charsmax(szMessage),g_szWelcomeMsgs[g_RandomMsg]); new const szMenuCodes[][]={"\r","\y","\w","\d","\R"}; for(new i=0;i<sizeof(szMenuCodes);i++)     replace_all(szMessage,charsmax(szMessage),szMenuCodes[i],""); client_print(id,print_chat,"bibu said %s to all",szMessage);

bibu 12-03-2010 05:50

Re: Replacing a part of a message
 
Works perfectly, thanks exolent. Couldn't understand seta's message, but however. :mrgreen:

hleV 12-03-2010 10:22

Re: Replacing a part of a message
 
Quote:

Originally Posted by bibu (Post 1361940)
Couldn't understand seta's message, but however. :mrgreen:

He meant that Exolent is using too much spacing in his code. While I also don't see how such spacing can look good or improve readability for anyone, it's a matter of one's taste.

Also it would be more efficient to not use 2D arrays for such code. Here's my way tho:
Code:
new message[sizeof(g_szWelcomeMsgs[])]; copy(message, charsmax(message), g_szWelcomeMsgs[g_RandomMsg]);   RemoveAll(message, charsmax(message), "\r"); RemoveAll(message, charsmax(message), "\y"); RemoveAll(message, charsmax(message), "\w"); RemoveAll(message, charsmax(message), "\d"); RemoveAll(message, charsmax(message), "\R");   client_print(id, print_chat, "bibu said %s to all", message);   // ...   RemoveAll(message[], length, text[])         while (replace(message, length, text, "")) {}

Bugsy 12-03-2010 13:46

Re: Replacing a part of a message
 
I think he was making it easier to review/add/remove the replace-characters if needed, not so much that a 2d array was required; this allows the user to easily add/remove items without touching any of the other code. I don't think the CPU would take a hit using either method, though. Plus IMO his looks a little cleaner, to each his own.

fysiks 12-03-2010 13:48

Re: Replacing a part of a message
 
Quote:

Originally Posted by hleV (Post 1362038)
He meant that Exolent is using too much spacing in his code. While I also don't see how such spacing can look good or improve readability for anyone, it's a matter of one's taste.

Also it would be more efficient to not use 2D arrays for such code. Here's my way tho:
Code:
new message[sizeof(g_szWelcomeMsgs[])]; copy(message, charsmax(message), g_szWelcomeMsgs[g_RandomMsg]);   RemoveAll(message, charsmax(message), "\r"); RemoveAll(message, charsmax(message), "\y"); RemoveAll(message, charsmax(message), "\w"); RemoveAll(message, charsmax(message), "\d"); RemoveAll(message, charsmax(message), "\R");   client_print(id, print_chat, "bibu said %s to all", message);   // ...   RemoveAll(message[], length, text[])         while (replace(message, length, text, "")) {}

Why don't you just use replace_all()?

hleV 12-03-2010 17:16

Re: Replacing a part of a message
 
Why should I?

Arkshine 12-03-2010 18:23

Re: Replacing a part of a message
 
replace_all() uses more code and is supposed to be used to avoid infinite replacement, but here it won't happen, so it's fine while + replace. Btw, I agree with Bugsy.


All times are GMT -4. The time now is 11:19.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.