Jump to content
  • 0
Feilor

string in array key and switch case

Question

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;
}

 

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0
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 by Z3R0
Added Codebox
  • MVP 1
Link to comment
Share on other sites

  • 0

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

Link to comment
Share on other sites

  • 0

 

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!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.