Raised This Month: $51 Target: $400
 12% 

Split string


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
BeNq!
Senior Member
Join Date: Mar 2009
Old 07-08-2017 , 12:51   Split string
Reply With Quote #1

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

Last edited by BeNq!; 07-08-2017 at 12:55.
BeNq! is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 07-08-2017 , 17:35   Re: Split string
Reply With Quote #2

You simply need to parse the [item] text as a comma separated string list using strtok() or similar function.
__________________
fysiks is offline
BeNq!
Senior Member
Join Date: Mar 2009
Old 07-09-2017 , 01:44   Re: Split string
Reply With Quote #3

Are you giving an example?
BeNq! is offline
SpawnerF
Member
Join Date: Apr 2017
Location: Morocco
Old 07-09-2017 , 14:13   Re: Split string
Reply With Quote #4

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
__________________
XX was created just for giving evidence and not meant to damage public servers.
&We do not test on public servers.
Thank's.

Last edited by SpawnerF; 07-09-2017 at 21:07.
SpawnerF is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 07-09-2017 , 19:36   Re: Split string
Reply With Quote #5

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.
__________________

Last edited by fysiks; 07-09-2017 at 20:09.
fysiks is offline
SpawnerF
Member
Join Date: Apr 2017
Location: Morocco
Old 07-09-2017 , 19:57   Re: Split string
Reply With Quote #6

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.
__________________
XX was created just for giving evidence and not meant to damage public servers.
&We do not test on public servers.
Thank's.

Last edited by SpawnerF; 07-09-2017 at 19:58.
SpawnerF is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 07-09-2017 , 20:09   Re: Split string
Reply With Quote #7

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.
__________________

Last edited by fysiks; 07-09-2017 at 20:10.
fysiks is offline
SpawnerF
Member
Join Date: Apr 2017
Location: Morocco
Old 07-09-2017 , 20:57   Re: Split string
Reply With Quote #8

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.
__________________
XX was created just for giving evidence and not meant to damage public servers.
&We do not test on public servers.
Thank's.
SpawnerF is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 07-09-2017 , 21:22   Re: Split string
Reply With Quote #9

Quote:
Originally Posted by SpawnerF View Post
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 View Post
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?
__________________
fysiks is offline
SpawnerF
Member
Join Date: Apr 2017
Location: Morocco
Old 07-09-2017 , 21:39   Re: Split string
Reply With Quote #10

Yes it's working. You can try it.

edit : sizeof is a pre-processor function?
__________________
XX was created just for giving evidence and not meant to damage public servers.
&We do not test on public servers.
Thank's.

Last edited by SpawnerF; 07-09-2017 at 21:40.
SpawnerF 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 15:21.


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