On Thursday, 5 February 2015 at 12:31:31 UTC, Stéphane wrote:
Syntax for checking if an element exists in a list
Hello,
I would like to know if there is a better (easier to wite,
easier to read, easier to understand) way to check if an element
(string) is
in a list of strings.
Here are the possibilities I see today, as someone who is new
to D:
1) using a switch
char [] myopt=get_an_option();
switch(myopt) {
case "opt1", "opt2", "opt3":
do_A();
break;
case "opt4":
do_B();
break;
default:
do_C();
}
Note that D does NOT have default fall through. i.e.
switch(myopt)
{
case "opt1", "opt2", "opt3":
do_A();
break;
case "opt4":
do_B();
break;
default:
do_C();
}
is equivalent to
switch(myopt)
{
case "opt1", "opt2", "opt3":
do_A();
case "opt4":
do_B();
default:
do_C();
}
to enable fall through use goto case; / goto case n; to fall
through once (to the next case) and to case n (where n is the
case label identifier i.e. 5 or "foo" or mylabel: )
switch(myopt)
{
case "opt1", "opt2", "opt3":
do_A();
goto case;
case "opt4":
do_B();
default:
do_C();
}
PPS: Where should I post, when I have such questions and
problems
as the one in my previous paragraph? I did not find any "meta"
forum.
learn is the correct place for such questions and in general for
questions about how to do something or enquire as to why
something is not working or not working the way you think it
should.
Announcement is for announcements.
debuggers and ide are for debuggers and ide.
digitalmars.D is for discussion about the language the libraries
and links to interesting stuff (for some value of stuff)
the D.gnu and D.ldc are for discussions/queries about gdc and ldc
(the gcc and llvm backend compilers)