AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   parse string with delimeter (https://forums.alliedmods.net/showthread.php?t=143608)

-=hunter=- 11-22-2010 00:33

parse string with delimeter
 
Hello. I searched parse function with delimeter like parse() but not found. Maybe there is a ready function?
I'm need function like this: parse2(text[], delimeter[1], arg1, len1, arg2, len2, ...)

Exolent[jNr] 11-22-2010 00:38

Re: parse string with delimeter
 
Use strtok().

-=hunter=- 11-22-2010 12:18

Re: parse string with delimeter
 
Exolent[jNr] Thx for help.

I did not know how to do function with dynamic number of arguments (searched best way but not found) therefore I did fuction with fixed number of arguments. Probable it will be useful for somebody.

Example:
PHP Code:

new gTempText[5000]
...
public 
parse6(const text[], delimeterarg1[], arg2[], arg3[], arg4[], arg5[], arg6[])
{
    
copy(gTempText4999text)
        
    
strtok(gTempTextarg1999gTempText9999delimeter)
    if (
gTempText[0] == '^0') return 1
    
    strtok
(gTempTextarg2999gTempText9999delimeter)
    if (
gTempText[0] == '^0') return 2
    
    strtok
(gTempTextarg3999gTempText9999delimeter)
    if (
gTempText[0] == '^0') return 3
    
    strtok
(gTempTextarg4999gTempText9999delimeter)
    if (
gTempText[0] == '^0') return 4
    
    strtok
(gTempTextarg5999gTempText9999delimeter)
    if (
gTempText[0] == '^0') return 5
    
    strtok
(gTempTextarg6999gTempText9999delimeter)

    return 
6


Works slower than parse() in ~2.4 times.

Emp` 11-22-2010 13:37

Re: parse string with delimeter
 
Untested:
Code:

stock parsetok(const text[], token='', trimSpaces=0, ...)
{
        new argnum = numargs();
        if( argnum < 5 )
                return 0;

        new _text[512], _left[256], _len, _arg, _i;
        copy( _text, charsmax(_text), text );
        do
        {
                _len = min( charsmax(_left), getarg( 4 + (2*_arg) ) );
                strtok( _text, _left, _len, _text, charsmax(_text), token, trimSpaces );

                for( _i = 0; _i < _len && _left[_i] != '^0'; _i++ )
                {
                        setarg( 3 + (2*_arg), _i, _left[_i] );
                }
                if( arg >= argnum )
                        break;
                _arg++;
        }
        while( _text[0] != '^0' )

        return _arg;
}


Exolent[jNr] 11-22-2010 14:02

Re: parse string with delimeter
 
Quote:

Originally Posted by Emp` (Post 1354553)
Untested:
Code:

stock parsetok(const text[], token='', trimSpaces=0, ...)

IIRC you cannot use '' for a character.
You must give a character or number.

EDIT:

Here's my version without limits within the function:
Code:
stock parsetok(const text[], token=' ', trimSpaces=0, ...) {     new iArgCount = numargs( );     if( iArgCount < 5 )     {         return 0;     }         new szFind[ 2 ];     szFind[ 0 ] = token;         new iTextLen = strlen( text );     new iStart, iStop;     new iArgStringIndex = 3;     new iArgLenIndex = 4;     new iParseCount;     new iArgLen;     new i;         while( iArgStringIndex < iArgCount && iArgLenIndex < iArgCount && iStart < iTextLen )     {         while( trimSpaces && text[ iStart ] == ' ' && iStart < iTextLen )         {             iStart++;         }                 iStop = contain( text[ iStart ], szFind ) + iStart;                 if( iStop < iStart )         {             iStop = iTextLen;         }                 iArgLen = getarg( iArgLenIndex );                 if( iArgLen > 0 )         {             for( i = 0; i < iArgLen; i++ )             {                 setarg( iArgStringIndex, i, text[ i + iStart ] );             }                         setarg( iArgStringIndex, iArgLen, EOS );         }                 iStart = iStop + 1;                 iArgStringIndex += 2;         iArgLenIndex += 2;         iParseCount++;     }         return iParseCount; }

-=hunter=- 11-23-2010 00:45

Re: parse string with delimeter
 
Exolent[jNr] I tried but for me dont works correctly.

PHP Code:

new arg1[51], arg2[51], arg3[51], arg4[51], arg5[51], arg6[51]
new 
text[130] = "Player1;Player2;;Player4;Player5;Player6"
parsetok(text';'1arg150arg250arg3,50arg450,arg550arg650)
server_print("parsed: arg1 = %s, arg2 = %s, arg3 = %s, arg4 = %s, arg5 = %s, arg6 = %s"arg1arg2arg3arg4arg5arg6)

// parsed: arg1 =  , arg2 = Player2, arg3 = Player4, arg4 = Player6, arg5 = , arg6 = 

Found errors. Must be:
new iArgStringIndex = 3;
new iArgLenIndex = 4;
iArgStringIndex += 2;
iArgLenIndex += 2;

Exolent[jNr] Big thanks :) This way works is a little faster than with strtok

Exolent[jNr] 11-23-2010 01:51

Re: parse string with delimeter
 
Quote:

Originally Posted by -=hunter=- (Post 1354914)
Exolent[jNr] I tried but for me dont works correctly.

PHP Code:

new arg1[51], arg2[51], arg3[51], arg4[51], arg5[51], arg6[51]
new 
text[130] = "Player1;Player2;;Player4;Player5;Player6"
parsetok(text';'1arg150arg250arg3,50arg450,arg550arg650)
server_print("parsed: arg1 = %s, arg2 = %s, arg3 = %s, arg4 = %s, arg5 = %s, arg6 = %s"arg1arg2arg3arg4arg5arg6)

// parsed: arg1 =  , arg2 = Player2, arg3 = Player4, arg4 = Player6, arg5 = , arg6 = 

Found errors. Must be:
new iArgStringIndex = 3;
new iArgLenIndex = 4;
iArgStringIndex += 2;
iArgLenIndex += 2;

Exolent[jNr] Big thanks :) This way works is a little faster than with strtok

Has noticed that getarg works faster, than strlen in ~4 times

I forgot that arguments start at 0, not 1. And yes, I incremented wrongly.
Code has been fixed.

-=hunter=- 11-23-2010 03:57

Re: parse string with delimeter
 
Also it is necessary to add
PHP Code:

setargiArgStringIndexiArgLen'^0' ); 

so function can be called with the same arguments some times with other text

Exolent[jNr] 11-23-2010 08:47

Re: parse string with delimeter
 
Quote:

Originally Posted by -=hunter=- (Post 1354959)
Also it is necessary to add
PHP Code:

setargiArgStringIndexiArgLen'^0' ); 

so function can be called with the same arguments some times with other text

Yes, you're right. More than that was needed so see function for changes.

Emp` 11-23-2010 15:46

Re: parse string with delimeter
 
Quote:

Originally Posted by Exolent[jNr] (Post 1354569)
IIRC you cannot use '' for a character.
You must give a character or number.

You're right, I looked at strtok on the funcwiki really quick and assumed it was ''.


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

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