Feilor Posted August 18, 2017 Posted August 18, 2017 Hi! Can i use strings in array and switch case keys? Like this: Array: setarray @array["string"],0,1,2,3,4 Switch: switch(@string$){ case "string": break; case "string2": break; } Quote
0 Z3R0 Posted August 19, 2017 Posted August 19, 2017 (edited) 7 hours ago, luan122 said: Hi! Can i use strings in array and switch case keys? Like this: Array: setarray @array["string"],0,1,2,3,4 Switch: switch(@string$){ case "string": break; case "string2": break; } 1st one can be "simulated" setarray( getd( "@array_" + "string"), 0, 1, 2, 3, 4; ); // this will give you @array_string[#] 2nd, I found this out earlier, no, switch does not work with strings..., however what I found is that if you are setting strings for a switch, something is wrong... You can usually use a lookup array for the string and determine which index value it is in order to "case" it properly Edited August 19, 2017 by Z3R0 Added Codebox 1 Quote
0 randell1993 Posted August 19, 2017 Posted August 19, 2017 Don't think so as in the documentation *switch (expression); The switch statement is similar to a series of if statements on the same expression. In many occasions, you may want to compare the same variable (or expression) with many different values, and execute a different piece of code depending on which value it equals to. This is exactly what the switch statement is for. It is important to understand how the switch statement is executed in order to avoid mistakes. The switch statement executes line by line (actually, statement by statement). In the beginning, no code is executed. Only when a case statement is found with a value that matches the value of the switch expression the case statement(s) will to executed. The parser continues to execute the statements until the end of the switch block, or the first time it sees a break statement. If you don't write a break statement at the end of a case's statement list, the parser will go on executing the statements of the following case (fall-through). Example 1: switch(select("Yes:No")) { case 1: mes "You said yes!"; break; case 2: mes "Aww, why?"; break; } close; The example above would work like a menu and would go to the first case if the user selects option, otherwise, would go to the second one. Example 2: switch(getgroupid()) { case 1: mes "Wow, you're super!"; break; case 2: mes "A helping hand!"; break; case 3: mes "10001010010011"; break; case 4: mes "Yes, milord?"; break; default: mes "Hello there!"; break; } The example above would print a message depending on the player's groupid. If there is no statement declared for the corresponding groupid, the script would use the 'default' statement that applies to rest of possible values, similar to 'else' in the if-else statement. I haven't read the source for this but just try it maybe it may work Quote
0 Feilor Posted August 19, 2017 Author Posted August 19, 2017 10 hours ago, Z3R0 said: 1st one can be "simulated" setarray( getd( "@array_" + "string"), 0, 1, 2, 3, 4; ); // this will give you @array_string[#] 2nd, I found this out earlier, no, switch does not work with strings..., however what I found is that if you are setting strings for a switch, something is wrong... You can usually use a lookup array for the string and determine which index value it is in order to "case" it properly I asked just to have sure i changed the way i'm using arrays and switch but using strings sometimes would be very usefull, thank you! Quote
Question
Feilor
Hi!
Can i use strings in array and switch case keys? Like this:
Array:
Switch:
3 answers to this question
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.