AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved How use split_string? (https://forums.alliedmods.net/showthread.php?t=322367)

+ARUKARI- 03-24-2020 20:38

How use split_string?
 
I wrote the following code.
But output all zero..
PHP Code:

new color[3];
new 
sColor[12]; // max 255,255,255
new sPart[4];
new 
sPartLen  charsmax(sPart);
new 
sColorLen charsmax(sColor);

get_pcvar_string(COLOR_CTsColorsColorLen); // COLOR_CT = 0,0,255

new 00ridx 0;
while((
split_string(sColor[ridx], ","sPartsPartLen)) != -1)
{
    
ridx += i;
    
color[n++] = str_to_num(sPart);
    
server_print(sPart); // All zero....


Is this usage correct?

Bugsy 03-24-2020 20:52

Re: How use split_string?
 
Show what the source string looks like and what you expect to parse from it.

JusTGo 03-24-2020 20:52

Re: How use split_string?
 
https://www.amxmodx.org/api/string/split_string

From the documentation split_string should accept the source string, but you are giving it only one letter
try to change it to :

split_string(sColor, ",", sPart, sPartLen))

Bugsy 03-24-2020 20:59

Re: How use split_string?
 
PHP Code:

new szRGB[] = "251,252,253";
new 
szR] , szG] , szB];
new 
RGB];
    
replace_allszRGB charsmaxszRGB ) , "," " " );
parseszRGB szRcharsmaxszR ) , szG charsmaxszG ) , szB charsmaxszB ) );

RGB] = str_to_numszR );
RGB] = str_to_numszG );
RGB] = str_to_numszB ); 


fysiks 03-24-2020 21:47

Re: How use split_string?
 
Quote:

Originally Posted by JusTGo (Post 2688413)
https://www.amxmodx.org/api/string/split_string

From the documentation split_string should accept the source string, but you are giving it only one letter
try to change it to :

split_string(sColor, ",", sPart, sPartLen))

Technically, when you pass an indexed string to a function that expects a string, it will pass the string starting from the indexed location until the end of the string. E.g.

PHP Code:

new szString[] = "Hello World!"
server_print("%s"szString[6]) 

will result in "World!".

Bugsy 03-24-2020 21:54

Re: How use split_string?
 
Yea, to make this work you'd need to format it like "255,255,255,"
PHP Code:

new szRGBString[] = "251,252,253,";
new 
szRGB][ ];
new 
iPos;
    
while ( 
iPos sizeofszRGB ) )
{
    
+= split_stringszRGBString] , "," szRGBiPos++ ] , charsmaxszRGB[] ) );
}
    
server_print"%s %s %s" ,szRGB] , szRGB] , szRGB] ); 


+ARUKARI- 03-24-2020 22:02

Re: How use split_string?
 
Quote:

Originally Posted by Bugsy (Post 2688420)
Yea, to make this work you'd need to format it like "255,255,255,"
PHP Code:

new szRGBString[] = "251,252,253,";
new 
szRGB][ ];
new 
iPos;
    
while ( 
iPos sizeofszRGB ) )
{
    
+= split_stringszRGBString] , "," szRGBiPos++ ] , charsmaxszRGB[] ) );
}
    
server_print"%s %s %s" ,szRGB] , szRGB] , szRGB] ); 


oh wait..
last comma required?

0,0,255 => output:0 0 0(N/A)
0,0,255,=> output:0 0 255 yeah?!

Well, I'll try it when I get home.

Bugsy 03-24-2020 22:32

Re: How use split_string?
 
Using split_string(), yes. I personally would use my code using parse() above.

+ARUKARI- 03-25-2020 06:08

Re: How use split_string?
 
OK working.

Pattern A:
PHP Code:

new tcolor    [3];
new 
sRGB    [13];
new 
sColor[4];
new 
sRGBLen     charsmax(sRGB);
new 
sColorLen    charsmax(sColor[]);
new 
00iPos 0;

get_pcvar_string(COLOR_CTsRGBsRGBLen);
formatex(sRGBsRGBLen"%s%s"sRGB",");

while(
sizeof(tcolor))
{
    
split_string(sRGB[iPos += i], ","sColorsColorLen);
    
tcolor[n++] = str_to_num(sColor);


Pattern B:
PHP Code:

new tcolor    [3];
new 
sRGB    [13];
new 
sColor[3][4];
new 
sRGBLen     charsmax(sRGB);
new 
sColorLen    charsmax(sColor[]);

get_pcvar_string(COLOR_CTsRGBsRGBLen);

replace_all(sRGBsRGBLen","" ");
parse(sRGBsColor[0], sColorLensColor[1], sColorLensColor[2], sColorLen);
while(
sizeof(tcolor))
    
tcolor[n] = str_to_num(sColor[n++]); 

Pattern B is simply.
thanks all.
I knew how to use it so I solved it.


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

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