AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Try to replace... (https://forums.alliedmods.net/showthread.php?t=171949)

Shadow Of Death 11-12-2011 22:01

Try to replace...
 
I try to replace . with _ in a string.
But i dont know what i do wrong.
Hope any can help me with this. I use the stock from Exolent http://forums.alliedmods.net/showthread.php?t=163205.

PHP Code:

#include <amxmodx>
#include <amxmisc>
#include <string_stocks>
.
.
.
new 
server_address[2][33];

public 
plugin_init() {
    
register_plugin(PLUGINVERSIONAUTHOR);
    
register_clcmd("say /test","test")
    
    
get_cvar_string("ip"server_address[0], 32);
    
get_cvar_string("port"server_address[1], 32);
}

public 
test()
{
    
str_replace(server_address[0], 32".""_"01);
    
    
server_cmd("say IP: %s"server_address[0]);
    
server_cmd("say Port: %s"server_address[1]);
    return 
PLUGIN_CONTINUE;


it dosnt replace the dots.

fysiks 11-12-2011 23:23

Re: Try to replace...
 
Did you even try the most basic function yet? You should try the simplest functions first. replace_all() should work fine.

Napoleon_be 11-13-2011 07:22

Re: Try to replace...
 
I don't think you should even use "_" but just simply type _ and .

Bugsy 11-13-2011 11:22

Re: Try to replace...
 
Quote:

Originally Posted by Napoleon_be (Post 1595853)
I don't think you should even use "_" but just simply type _ and .

Wrong. Know what you're talking about if you're going to try to help others. You will end up just confusing them even more.

PHP Code:

replace_all string[], lenwhat[], with[] ) 

_ and . are symbols and the code would not compile. var[] means an array/string is expected.

PHP Code:

replace_allserver_address] , strlenserver_address] ) , "." "_" 


Shadow Of Death 11-13-2011 13:03

Re: Try to replace...
 
Thx again Bugsy,

think i ask more next time... try to code my first bigger plugin.
It work. But you know why the str_replace not work?

Bugsy 11-13-2011 13:11

Re: Try to replace...
 
Quote:

Originally Posted by Shadow Of Death (Post 1596053)
Thx again Bugsy,

think i ask more next time... try to code my first bigger plugin.
It work. But you know why the str_replace not work?

Not sure, I don't feel like looking at the stock. Just use replace_all(), it's less natives anyway.

fysiks 11-13-2011 21:21

Re: Try to replace...
 
Quote:

Originally Posted by Shadow Of Death (Post 1596053)
Thx again Bugsy,

think i ask more next time... try to code my first bigger plugin.
It work. But you know why the str_replace not work?

You seemed to have used it correctly but I couldn't figure out why it wouldn't work with your code.

Emp` 11-13-2011 23:41

Re: Try to replace...
 
Quote:

Originally Posted by Bugsy (Post 1596063)
Not sure, I don't feel like looking at the stock. Just use replace_all(), it's less natives anyway.

Just a reminder, replace_all is not a native, but actually a stock:
Code:

/* Replaces a contained string iteratively.
 * This ensures that no infinite replacements will take place by
 *  intelligently moving to the next string position each iteration.
 */
stock replace_all(string[], len, const what[], const with[])
{
        new pos = 0;
       
        if ((pos = contain(string, what)) == -1)
        {
                return 0;
        }
       
        new total = 0;
        new with_len = strlen(with);
        new diff = strlen(what) - with_len;
        new total_len = strlen(string);
        new temp_pos = 0;
       
        while (replace(string[pos], len - pos, what, with) != 0)
        {
                /* jump to position after replacement */
                pos += with_len;
               
                /* update cached length of string */
                total_len -= diff;
               
                /* will the next call be operating on the last character? */
                if (pos >= total_len)
                {
                        break;
                }
               
                /* find the next position from our offset */
                temp_pos = contain(string[pos], what);
               
                /* if it's invalid, we're done */
                if (temp_pos == -1)
                {
                        break;
                }
               
                /* otherwise, reposition and update counters */
                pos += temp_pos;
                total++;
        }
       
        return total;
}

But your point still stands.

Shadow Of Death 11-15-2011 18:55

Re: Try to replace...
 
Quote:

Originally Posted by fysiks (Post 1596301)
...used it correctly .... it wouldn't work with your code.

Mysterious, that scares me.

fysiks 11-16-2011 02:46

Re: Try to replace...
 
Quote:

Originally Posted by Shadow Of Death (Post 1597177)
Mysterious, that scares me.

Lol. It's probably something simple that's been overlooked. The question is: overlooked by whom?


All times are GMT -4. The time now is 08:27.

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