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

Switch Statement Comparing strings


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Avitar
Junior Member
Join Date: Feb 2005
Old 03-12-2009 , 01:46   Switch Statement Comparing strings
Reply With Quote #1

Hey all,

I read the wiki about using the switch statement and use it frequently in other languages I code in but this has me puzzled; I am using a switch with a string argument to do mass comparisons but receive errors. I think my problems here are the crappy way that smalltalk/source mod handles data types. Also i know you have to use a function to do a string comparison ie. strcmp -- so I am not sure what hack to use, but needless to say this doesn't work as expected:

Code:
switch(str){
    case "apple":{PrintToChatAll("\x04Apples");break; }
    case "banana":{PrintToChatAll("\x04Banana");;break; }
    case "beer":{PrintToChatAll("\x04Beer");;break; }
    case "milk":{PrintToChatAll("\x04Milk");;break; }
    default:{PrintToChatAll("\x04Fail");} 
}
Edit: oops thought I clicked on source mod scripting, can someone put it in that section?
__________________
Programming Mantra: New errors indicate progress.
Avitar is offline
naris
AlliedModders Donor
Join Date: Dec 2006
Old 03-12-2009 , 17:13   Re: Switch Statement Comparing strings
Reply With Quote #2

switch won't work with strings. You are going to have to use if else if.

Also, pawn doesn't use break with switch statements.

Code:
if (StrEqual(str, "apple"))
    PrintToChatAll("apple");
else if (StrEqual(str, "banana"))
    PrintToChatall("banana");
else if (StrEqual("str, "beer"))
    PrintToCharAll("beer");
else if (StrEqual(str, "milk"))
    PrintToCharAll("milk")
else
    PrintToChatAll("fail");
naris is offline
Avitar
Junior Member
Join Date: Feb 2005
Old 03-13-2009 , 21:31   Re: Switch Statement Comparing strings
Reply With Quote #3

blah, i guess it is time to fall back to spaghetti code,
__________________
Programming Mantra: New errors indicate progress.
Avitar is offline
danielkza
AMX Mod X Plugin Approver
Join Date: May 2007
Location: São Paulo - Brasil
Old 03-13-2009 , 21:36   Re: Switch Statement Comparing strings
Reply With Quote #4

Quote:
Originally Posted by Avitar View Post
blah, i guess it is time to fall back to spaghetti code,
You can always loop through an array if you have too much strings to deal with.
__________________

Community / No support through PM
danielkza 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 10:04.


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