AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Parse string cvar (https://forums.alliedmods.net/showthread.php?t=133501)

polodu94 07-26-2010 06:54

Parse string cvar
 
Hello,

I want to use this function ( parse( ) ) but i don't now how to use it.

I have my string cvar :

PHP Code:

16000 800 14000 

This cvar can be vary between 0 and 24 values ( ir we have 3 values ^^ )

I want to get back the values but i don't know how when the number of values vary.

PS : Sorry for my english

Thank you for Answers

Arkshine 07-26-2010 07:05

Re: Parse string cvar
 
http://www.amxmodx.org/funcwiki.php?go=func&id=58 you have an exemple. Also you can seach, you will find numerous others exemples.

polodu94 07-26-2010 07:08

Re: Parse string cvar
 
Yes but ir we know the number of arg.

Me i don't know the number of arg can be vary ( 0 of 24 )

If the number can't be vary yes the exaemple is good for me but it's not the case

Arkshine 07-26-2010 07:26

Re: Parse string cvar
 
It doesn't matter. You can still used parse(). But it won't be appropriate.
You could use strbreak(), looping until the right output is empty. Something like that.

polodu94 07-26-2010 07:41

Re: Parse string cvar
 
Arkshine thank you for help but i prefere with exemple can you make it because i don't understand you explications.

With my exemples, with the randomnumber of arg

Plz

G[o]Q 07-26-2010 16:46

Re: Parse string cvar
 
try this:
Code:

parse_item(string[],len,output[][]){
new bufor[1024];
new ile=0
for(new i=0 ;i<strlen(string);i++){
        if(!equal(string[i]," ")){
                format(bufor,1023,"%s%c",bufor,string[i]);
        }
        else
        {
                copy(output[ile],63,bufor);
                bufor = "";
                ile++
        }

}
return ile
}

Use
Code:

new string[]="asda dadawd addadaw"
new out[24][64]
new number= parse_item(string,sizeof string,output)

after use

Code:

out[0]="asda"
out[1]="dadawd"
out[2]="addadaw"

function return number of parsed items you can use this in
Code:

for(new i=0;i<number;i++) client_print(0,print_chat,"%s",out[i])

polodu94 07-27-2010 06:05

Re: Parse string cvar
 
g[o]q it's that i search thank you

Arkshine 07-27-2010 11:06

Re: Parse string cvar
 
Here my version :

Code:
ExplodeString ( const string[], output[], olen = sizeof output ) {     new len = strlen( string ); // We retrieve the length of the current string passed.         if ( !len )  { return 0; } // If the string is empty we stop there.     new i, c, j, count;     new number[ 12 ];         do     {         while ( string[ i ] == ' ' ) i++; // One or more spaces can be used between 2 numbers, so we move forward until we find a number.         while ( ( number[ j++ ] = c = string[ i++ ] ) && c != ' ' ) {} // We loop and save the number found until the next space found or end of string.         output[ count++ ] = str_to_num( number ); // We convert the number saved previously into a number and we stored in output at the slot count.         j = 0;     }     while ( i < len && count < olen ) // We should looping while we have not cross the string length.         return count; }

Code:
new myString[ 128 ]; get_pcvar_string( myPointer, myString, charsmax( myString ) ); new myNumbers[ 24 ]; new count = ExplodeString( myString, myNumbers ); if ( count ) {     log_amx( "Count = %d", count );         for ( new i; i < count; i++ )     {         log_amx( "%d - %d", i, myNumbers[ i ] );     } }

Example with :
Code:

"  54  1421  141 1 4622"
output :

Code:

[Untitled.amxx] Count = 5
[Untitled.amxx] 0 - 54
[Untitled.amxx] 1 - 1421
[Untitled.amxx] 2 - 141
[Untitled.amxx] 3 - 1
[Untitled.amxx] 4 - 4622


polodu94 07-27-2010 14:15

Re: Parse string cvar
 
nice too

Arkshine 07-27-2010 14:24

Re: Parse string cvar
 
Mine is better coded. :mrgreen: And also more suit for your need.


All times are GMT -4. The time now is 00:17.

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