Raised This Month: $ Target: $400
 0% 

parse string with delimeter


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
-=hunter=-
Senior Member
Join Date: Jul 2008
Old 11-22-2010 , 00:33   parse string with delimeter
Reply With Quote #1

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, ...)

Last edited by -=hunter=-; 11-22-2010 at 00:37.
-=hunter=- is offline
Send a message via ICQ to -=hunter=- Send a message via Skype™ to -=hunter=-
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 11-22-2010 , 00:38   Re: parse string with delimeter
Reply With Quote #2

Use strtok().
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
-=hunter=-
Senior Member
Join Date: Jul 2008
Old 11-22-2010 , 12:18   Re: parse string with delimeter
Reply With Quote #3

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.
-=hunter=- is offline
Send a message via ICQ to -=hunter=- Send a message via Skype™ to -=hunter=-
Emp`
AMX Mod X Plugin Approver
Join Date: Aug 2005
Location: Decapod 10
Old 11-22-2010 , 13:37   Re: parse string with delimeter
Reply With Quote #4

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;
}
Emp` is offline
Send a message via AIM to Emp` Send a message via MSN to Emp` Send a message via Yahoo to Emp` Send a message via Skype™ to Emp`
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 11-22-2010 , 14:02   Re: parse string with delimeter
Reply With Quote #5

Quote:
Originally Posted by Emp` View Post
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; }
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!

Last edited by Exolent[jNr]; 11-23-2010 at 08:46.
Exolent[jNr] is offline
Emp`
AMX Mod X Plugin Approver
Join Date: Aug 2005
Location: Decapod 10
Old 11-23-2010 , 15:46   Re: parse string with delimeter
Reply With Quote #6

Quote:
Originally Posted by Exolent[jNr] View Post
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 ''.
Emp` is offline
Send a message via AIM to Emp` Send a message via MSN to Emp` Send a message via Yahoo to Emp` Send a message via Skype™ to Emp`
-=hunter=-
Senior Member
Join Date: Jul 2008
Old 11-23-2010 , 00:45   Re: parse string with delimeter
Reply With Quote #7

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

Last edited by -=hunter=-; 11-23-2010 at 03:44.
-=hunter=- is offline
Send a message via ICQ to -=hunter=- Send a message via Skype™ to -=hunter=-
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 11-23-2010 , 01:51   Re: parse string with delimeter
Reply With Quote #8

Quote:
Originally Posted by -=hunter=- View Post
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.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
-=hunter=-
Senior Member
Join Date: Jul 2008
Old 11-23-2010 , 03:57   Re: parse string with delimeter
Reply With Quote #9

Also it is necessary to add
PHP Code:
setargiArgStringIndexiArgLen'^0' ); 
so function can be called with the same arguments some times with other text

Last edited by -=hunter=-; 11-23-2010 at 06:43.
-=hunter=- is offline
Send a message via ICQ to -=hunter=- Send a message via Skype™ to -=hunter=-
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 11-23-2010 , 08:47   Re: parse string with delimeter
Reply With Quote #10

Quote:
Originally Posted by -=hunter=- View Post
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.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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