AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   tokens (https://forums.alliedmods.net/showthread.php?t=117749)

ProIcons 02-04-2010 09:16

tokens
 
how can i take the first word of a variabled (from the : Lines[i] i want to take the first word, how can i take it?

example on mirc we have $gettok(This is a Sentence,2,32) = is
$gettok(SENTENCE,NUMBER OF WORD,CHARACTER (32 = space))

Seta00 02-04-2010 09:42

Re: tokens
 
Untested:
PHP Code:

stock gettok(string[], output[], nbOfWordchar) {
    new 
wordCountcurrentCharstart = -1i;
    for (
0strlen(string); ++i) {
        
currentChar string[i];
        if (
currentChar == char)
            
wordCount++;
        if (
wordCount == nbOfWord) {
            
start i;
            break;
        }
    }

    if (
start == -1)
        return -
1;

    while(
currentChar != char)
        
currentChar string[i++];

    
formatex(outputstart"%s"string[start]);
    return 
0;


new szTok[128];
gettok("this is a sequence", szTok, 2, 32);

ProIcons 02-04-2010 09:52

Re: tokens
 
Welcome to the AMX Mod X 1.8.1-300 Compiler.
Copyright (c) 1997-2006 ITB CompuPhase, AMX Mod X Team

Error: Invalid function or declaration on line 184
Error: Invalid expression, assumed zero on line 188
Error: Invalid expression, assumed zero on line 199
Error: Number of arguments does not match definition on line 221

4 Errors.



Could not locate output file D:\E\Backup STORE F\Counter Strike Servers\elajtjumperz\cstrike\addons\amxmodx\A mxx Script\Gather-Test.amx (compile failed).

Line: 184
PHP Code:

stock gettok(string[], output[], nbOfWordchar) { 

Line: 188
PHP Code:

if (currentChar == char

Line: 199
PHP Code:

while(currentChar != char

Line: 221
PHP Code:

gettok(lines[i], subtitue232); 


ProIcons 02-04-2010 10:09

Re: tokens
 
PHP Code:

stock gettok(string[], output[], nbOfWordchsar) {
    new 
wordCountcurrentCharstart = -1i;
    for (
0strlen(string); ++i) {
        
currentChar string[i];
        if (
currentChar == chsar)
            
wordCount++;
        if (
wordCount == nbOfWord) {
            
start i;
            break;
        }
    }

    if (
start == -1)
        return -
1;

    while(
currentChar != chsar)
        
currentChar string[i++];

    
formatex(outputstart"%s"string[start]);
    return 
0;


Because Char was green when i am replaced it with chsar it worked on Compiling but in the game is not working

PHP Code:

new subtitue[32];
            
gettok(lines[i], subtitue232); 
            
client_print(0,print_chat,"[Gather] Substitution Found, (%s) Has recived the password and connecting to the server",subtitue


Exolent[jNr] 02-04-2010 15:42

Re: tokens
 
This should work.
PHP Code:

stock GetToken( const szString[ ], const iToken, const cDelimiterszOutput[ ], const iLen )
{
    if( !
szString]
    || !
iToken
    
|| !( 0x01 <= cDelimiter <= 0xFF )
    {
        return 
0;
    }
    
    new 
iStart = -1iStop = -1;
    if( 
iToken )
    {
        new 
iCount;
        
        new 
cChariIndex = -1;
        while( ( 
cChar szString[ ++iIndex ] ) )
        {
            if( 
cChar == cDelimiter )
            {
                
iCount++;
            }
        }
        
        new 
iStringLen iIndex;
        
        if( 
iCount iToken )
        {
            
iCount iToken;
        }
        
        
iIndex 0;
        
        while( ( 
cChar szString[ ++iIndex ] ) )
        {
            if( 
cChar == cDelimiter )
            {
                if( !
iCount )
                {
                    
iStop iIndex;
                    
                    break;
                }
                else if( !--
iCount )
                {
                    
iStart iIndex 1;
                }
            }
        }
        
        if( 
iStart != -&& iStop == -)
        {
            
iStop iStringLen;
        }
    }
    else
    {
        new 
iCount;
        
        new 
cChariIndex = -1;
        while( ( 
cChar szString[ ++iIndex ] ) )
        {
            if( 
cChar == cDelimiter )
            {
                
iCount--;
            }
        }
        
        new 
iStringLen iIndex;
        
        if( 
iCount iToken )
        {
            
iCount iToken;
        }
        
        
iCount absiCount );
        
        while( ( 
cChar szString[ --iIndex ] ) )
        {
            if( 
cChar == cDelimiter )
            {
                if( !
iCount )
                {
                    
iStart iIndex 1;
                    
                    break;
                }
                else if( !--
iCount )
                {
                    
iStop iIndex;
                }
            }
        }
        
        if( 
iStop != -&& iStart == -)
        {
            
iStart 0;
        }
    }
    
    return 
copyszOutputminiStop iStartiLen ), szStringiStart ] );



Seta00 02-04-2010 18:05

Re: tokens
 
Simplified (working) version:
PHP Code:

stock gettok(output[], string[], nbOfWorddelimiter) {
    if (!
string[0]|| !(0x01 <= delimiter <= 0xFF) || !nbOfWord// does Pawn support unicode???
        
return 0;
        
    new 
iendwordCount 1;
    for (
0strlen(string); i++) {
        if (
string[i] == delimiter)
            
wordCount++;
        if (
wordCount == nbOfWord)
            break;
    }
    
    for (
end = ++iend strlen(string); end++) {
        if (
string[end] == delimiter)
            break;
    }
    
    return 
copy(outputend-i+1string[i]);


nbOfWord starts at 1, so to get the first token: gettok(output, string, 1, 32)

Bugsy 02-04-2010 21:38

Re: tokens
 
Quote:

Originally Posted by Seta00 (Post 1078630)
Simplified (working) version:

If you try to retrieve the first token with your code the first letter gets dropped and spaces are left on words.
Code:

"This is an example"

Word 1 | "his " | Length=4
Word 2 | "is " | Length=3
Word 3 | "an " | Length=3
Word 4 | "example" | Length=7

Here's a working version
PHP Code:

stock GetTokenszOutput[] , const szSource[] , iWordNumber cDelimiter 
{
    if ( !
szSource] || !( <= cDelimiter <= 255 ) || !iWordNumber )
        return 
0;
        
    new 
iPos iStart iLen strlenszSource ) , iWordCount 1;
    
    for ( 
iPos iPos iLen iPos++ ) 
    {
        if ( 
szSourceiPos ] == cDelimiter 
        {
            if ( 
iWordCount == iWordNumber )
            {
                 break;
            }
            else 
            {
                
iWordCount++;
                
iStart iPos 1;
            }
        }
    }
    
    return 
copyszOutput , ( iPos iStart ) , szSourceiStart ] );


Example:
PHP Code:

new szTest[] = "This is an example";
new 
szWord];
    
for ( new 
<= i++ )
    
server_print"Word %d | ^"%s^" | Length=%d" szWord GetTokenszWord szTest ' ' ) ); 

Result:
Code:

Word 1 | "This" | Length=4
Word 2 | "is" | Length=2
Word 3 | "an" | Length=2
Word 4 | "example" | Length=7


Seta00 02-05-2010 07:13

Re: tokens
 
1 Attachment(s)
I forgot to put quotes around the string, so I didn't see the extra space. Sorry :oops:
The problem was the check for nbOfWords, here's the correct code:
PHP Code:

stock gettok(output[], string[], nbOfWorddelimiter) {
    if (!
string[0]|| !(0x01 <= delimiter <= 0xFF) || !nbOfWord// does Pawn support unicode???
        
return 0;
        
    new 
iendwordCount 1;
    for (
0strlen(string); i++) {
        if (
wordCount == nbOfWord)
            break;
        if (
string[i] == delimiter)
            
wordCount++;
    }
    
    for (
end i+1end strlen(string); end++) {
        if (
string[end] == delimiter)
            break;
    }
    
    return 
copy(outputend-istring[i]);



ProIcons 11-25-2010 10:21

Re: tokens
 
PHP Code:

stock gettok(output[], string[], nbOfWorddelimiter) {
    if (!
string[0]|| !(0x01 <= delimiter <= 0xFF) || !nbOfWord// does Pawn support unicode???
        
return 0;
        
    new 
iendwordCount 1;
    for (
0strlen(string); i++) {
        if (
wordCount == nbOfWord)
            break;
        if (
string[i] == delimiter)
            
wordCount++;
    }
    
    for (
end i+1end strlen(string); end++) {
        if (
string[end] == delimiter)
            break;
    }
    
    return 
copy(outputend-istring[i]);
}  



if (
equali(arg1,"ADDGAME"))
    {
       
set_variables();
       new 
targetmap[32],temp[32],targetplayers[2];
       
gettok(targetmap,botcmd,1,32);
       
gettok(Admin,botcmd,2,32);
       
gettok(targetplayers,botcmd,3,32);
       
players targetplayers[1];
       
server_cmd("amx_map %s",targetmap);
       for( new 
1<= playersi++ )
       {
           
gettok(temp,botcmdi,32)
           
format(player[i],31,temp);
       }
    } 


Line 380: if (string[end] == delimiter)

Quote:


[Gather] ADDGAME de_aztec ProIcons 4 ProIcons Casanova GaMeMaSteR Dev
L 11/25/2010 - 17:18:32: [AMXX] Displaying debug trace (plugin "gather.amxx")
L 11/25/2010 - 17:18:32: [AMXX] Run time error 5: memory access
L 11/25/2010 - 17:18:32: [AMXX] [0] Gather.sma::gettok (line 380)
L 11/25/2010 - 17:18:32: [AMXX] [1] Gather.sma::socket_read (line 484)

Bugsy 11-25-2010 10:23

Re: tokens
 
Try http://forums.alliedmods.net/showpos...53&postcount=7 ?


All times are GMT -4. The time now is 07:24.

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