AlliedModders

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

BeNq! 07-08-2017 12:51

Split string
 
Hello,

I have a code:

Code:

__loadFileClass( )
{
        new szFile[ 256 ];
       
        get_configsdir( szFile, 255 );
        format( szFile, 255, "%s/NameFile.ini", szFile );
       
        if( !file_exists( szFile ) )
                return;
       
        new szRow[ 256 ], iTrash,  iSize = file_size( szFile, 1 );
       
        for( new i = 0; i < iSize; i++ )
        {
                read_file( szFile, i, szRow, charsmax( szRow ), iTrash );
               
                if( ( contain( szRow, ";" ) != 0 ) && strlen( szRow ) )
                {
                        replace( szRow, charsmax( szRow ), "[class]", "" );
                        split( szRow, szClassNameBlock[ iLine ], charsmax( szClassNameBlock[ ] ), szItemNameBlock[ iLine ], charsmax( szItemNameBlock[ ] ), "[item]" );
                        iLine++;
                }
        } 
}

and

Code:

for( new i = 0; i < iLine; i++ )
        {
                if( equali( classBuffer[ ciName ], szClassNameBlock[ i ] ) && equali( itemBuffer[ iiName ], szItemNameBlock[ i ] ) )
                {
                        break;
                }
        }

NameFile.ini:

Code:

[class]Test[item]ItemName
[class]Test[item]ItemName2
[class]Test2[item]ItemName3

How can I make it work that way:

Code:

[class]Test[item]ItemName,ItemName2,...
[class]Test2[item]ItemName3

So for classes e.g. "Test" to be assigned ItemName, ItemName2...

if( equali( classBuffer[ ciName ], szClassNameBlock[ i ] ) && equali( itemBuffer[ iiName ], szItemNameBlock[ i ] ) )
Class Test | ItemName
if( equali( classBuffer[ ciName ], szClassNameBlock[ i ] ) && equali( itemBuffer[ iiName ], szItemNameBlock[ i ] ) )
Class Test | ItemName2

fysiks 07-08-2017 17:35

Re: Split string
 
You simply need to parse the [item] text as a comma separated string list using strtok() or similar function.

BeNq! 07-09-2017 01:44

Re: Split string
 
Are you giving an example?

SpawnerF 07-09-2017 14:13

Re: Split string
 
You can use something like that

Spoiler


Edit :
Output

Code:

] parse
Class name : myClass1
        [+] Item name : item1
        [+] Item name : item2
        [+] Item name : item3
Class name : myClass2
        [+] Item name : 2_item1
        [+] Item name : 2_item2
        [+] Item name : 3_item3
Class name : myClass3
        [+] Item name : 3_item1
        [+] Item name : 3_item2
        [+] Item name : 3_item3


fysiks 07-09-2017 19:36

Re: Split string
 
SpawnerF, please stop trying to proliferate your dot notation. It provided little if any benefit, is confusing (especially for new coders), and completely unnecessary in this language.

SpawnerF 07-09-2017 19:57

Re: Split string
 
I never said that it's important, it's just my way of coding, sorry if it does bother you but I like it like that.

fysiks 07-09-2017 20:09

Re: Split string
 
Strings should never be returned from functions especially general purpose functions.

I'm not sure what angle brackets do for creating an array but it's not wise to use extremely obscure features when providing support on these forums unless it's the only way to do it.

containi() returns the position in the string where the search string starts. If it is not found, it will return -1. So "~containi()" will evaluate as True when the string is NOT found and when the string does not start at the beginning.

SpawnerF 07-09-2017 20:57

Re: Split string
 
Code:

Strings should never be returned from functions especially general purpose functions.
What do you suggest then?

Code:

I'm not sure what angle brackets do for creating an array but it's not wise to use extremely obscure features when providing support on these forums unless it's the only way to do it.
It's a macro replaced by the pre-processor, I add it because when I do charsmax(p_szOutput[]) I get an error saying "indeterminate array size" but nvm It's fixed I updated my code adding the size parameters using sizeof in Split func.

fysiks 07-09-2017 21:22

Re: Split string
 
Quote:

Originally Posted by SpawnerF (Post 2534352)
Code:

Strings should never be returned from functions especially general purpose functions.
What do you suggest then?

Strings should be passed as byref function parameters the same way that every AMX Mod X function that generates a string as its output. For example, get_user_name() takes the string array and the length as parameters.


Quote:

Originally Posted by SpawnerF (Post 2534352)
Code:

I'm not sure what angle brackets do for creating an array but it's not wise to use extremely obscure features when providing support on these forums unless it's the only way to do it.
It's a macro replaced by the pre-processor, I add it because when I do charsmax(p_szOutput[]) I get an error saying "indeterminate array size" but nvm It's fixed I updated my code adding the size parameters using sizeof in Split func.

charsmax and sizeof are pre-processor functions so it is not possible to use them on arrays that don't have a determined size at compile time. Therefore, you can't use them on any variables in a function's prototype that doesn't have a defined size.

Given this, I don't know how your Split() function can actually work since you are using sizeof in the function's prototype. Not sure why it even compiles. If you need to know the dimensions of the arrays provided to the function, you'll need to pass those dimensions into the function as shown in the URL that you referenced.

Did you test your code?

SpawnerF 07-09-2017 21:39

Re: Split string
 
Yes it's working. You can try it.

edit : sizeof is a pre-processor function?


All times are GMT -4. The time now is 22:49.

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