Raised This Month: $32 Target: $400
 8% 

Solved More efficient strcmp check?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
kot4404
Senior Member
Join Date: Mar 2013
Old 01-28-2018 , 09:09   More efficient strcmp check?
Reply With Quote #1

Hello, how could I make the strcmp check more efficient? I have
Code:
public Action:Command_check(client, args)
{
	if(!strcmp(sCheck, "easy"))
	{
		PrintToChat(client, sCheck);
	}
	else if(!strcmp(sCheck, "normal"))
	{
		PrintToChat(client, sCheck);

	}
	else if(!strcmp(sCheck, "hard"))
	{
		PrintToChat(client, sCheck);
	}
	//do the things
}
Can I avoid making that many strcmp checks so Only one if check of easy & normal & hard is here?

Also how can I do it so it won't execute the //do the things unless it passed the strcmp check?
I tried to add the Plugin_Handled thing but it still executed rest of the functions

Last edited by kot4404; 01-28-2018 at 14:46.
kot4404 is offline
hmmmmm
Great Tester of Whatever
Join Date: Mar 2017
Location: ...
Old 01-28-2018 , 12:00   Re: More efficient strcmp check?
Reply With Quote #2

PHP Code:
if( StrEqualsCheck"easy"false ) || StrEqualsCheck"normal" ) || StrEqualsCheck"hard" ) )
{
    
PrintToChatclientsCheck );
    
// do the things

Bit unclear what you're trying to do, but I think this is what you want.
hmmmmm is offline
backwards
AlliedModders Donor
Join Date: Feb 2014
Location: USA
Old 01-28-2018 , 12:19   Re: More efficient strcmp check?
Reply With Quote #3

if it's a controlled string for instance from a menu string return value you can use single char compares that are unique identifiers like this:
PHP Code:
public Action:Command_check(clientargs)
{
    if(
sCheck[0] == 'e' || sCheck[0] == 'n' || sCheck[0] == 'h')
    {
        
PrintToChatclientsCheck );
    }
    
    return 
Plugin_Handled;
    
//do the things

when you return the code underneath within the same scope will not execute.
__________________
I highly recommend joining the SourceMod Discord Server for real time support.
backwards is offline
kot4404
Senior Member
Join Date: Mar 2013
Old 01-28-2018 , 13:44   Re: More efficient strcmp check?
Reply With Quote #4

Code:
if( StrEqual( sCheck, "easy", false ) || StrEqual( sCheck, "normal" ) || StrEqual( sCheck, "hard" ) )
{
    PrintToChat( client, sCheck );
    // do the things
}
Thanks, that's what I wanted
But now I tried to add opposite check so if there are 0 args or string isn't equal to easy normal hard it should display the error message

Code:
	if (args < 1 || !StrEqual( sDifficulty, "easy", false ) || !StrEqual( sDifficulty, "normal", false ) || !StrEqual( sDifficulty, "hard", false ))
	{
		ReplyToCommand(client, "Usage: !votedifficulty <difficulty> (easy;normal;hard)");
		return Plugin_Handled;
	}
But I can't pass the check even if I give the good string, any idea why?
kot4404 is offline
hmmmmm
Great Tester of Whatever
Join Date: Mar 2017
Location: ...
Old 01-28-2018 , 14:06   Re: More efficient strcmp check?
Reply With Quote #5

Step through it with some example input. Say you pass in 1 argument which is "easy". Args is 1, so first check is false. sDifficulty is "easy" so second check is false. sDifficulty is not normal, so third check returns true, and it enters the code block.

What you want is this instead:
PHP Code:
if (args || !(StrEqualsDifficulty"easy"false ) || StrEqualsDifficulty"normal" ) || StrEqualsDifficulty"hard" ))) 
This way if input is NOT "easy", "normal" or "hard" it returns false and enters like you want it to
hmmmmm is offline
kot4404
Senior Member
Join Date: Mar 2013
Old 01-28-2018 , 14:38   Re: More efficient strcmp check?
Reply With Quote #6

Quote:
Originally Posted by hmmmmm View Post
Step through it with some example input. Say you pass in 1 argument which is "easy". Args is 1, so first check is false. sDifficulty is "easy" so second check is false. sDifficulty is not normal, so third check returns true, and it enters the code block.

What you want is this instead:
PHP Code:
if (args || !(StrEqualsDifficulty"easy"false ) || StrEqualsDifficulty"normal" ) || StrEqualsDifficulty"hard" ))) 
This way if input is NOT "easy", "normal" or "hard" it returns false and enters like you want it to
Thanks now I got it
kot4404 is offline
Benoist3012
Veteran Member
Join Date: Mar 2014
Location: CWave::ForceFinish()
Old 01-29-2018 , 11:11   Re: More efficient strcmp check?
Reply With Quote #7

Quote:
Originally Posted by kot4404 View Post
Code:
if( StrEqual( sCheck, "easy", false ) || StrEqual( sCheck, "normal" ) || StrEqual( sCheck, "hard" ) )
{
    PrintToChat( client, sCheck );
    // do the things
}
Thanks, that's what I wanted
You realize that StrEqual is just a disguised strcmp?
Code:
stock bool StrEqual(const char[] str1, const char[] str2, bool caseSensitive=true) {     return (strcmp(str1, str2, caseSensitive) == 0); }

Your code is just:
Code:
if( strcmp( sCheck, "easy", false ) == 0 || strcmp( sCheck, "normal" ) == 0 || strcmp( sCheck, "hard" ) == 0 )
__________________
Benoist3012 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 14:53.


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