| Thread overview | |||||||||
|---|---|---|---|---|---|---|---|---|---|
|
February 05, 2015 Syntax for checking if an element exists in a list | ||||
|---|---|---|---|---|
| ||||
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();
}
A switch/case is nice when there are many cases, but when there
are only 1 or 2, the syntax is heavy (because of the "breaks")
and not
so nice (the "if"/"else" toggle is much more visible in my eyes
than the "case/break/default").
2) InExpression
It only works with hashes (with hashes keys, more exactly).
char [] myopt=get_an_option();
if(myopt in ["opt1":1, "opt2":1, "opt3":1]) {
do_A();
} else if(myopt=="opt4") {
do_B();
} else {
do_C();
}
The fact that the hash needs values means is not very nice to
read and write those useless (in this use case) dummy init values
":1" and
it brings not only visual confusion but also confusion about the
intention, especially if the hash is declared beforehand:
int[string] main_set_of_options=["opt1":1, "opt2":1, "opt3":1];
char [] myopt=get_an_option();
if(myopt in main_set_of_options) {
do_A;
} else if(myopt=="opt4") {
do_B();
} else {
do_C();
}
because it is impossible to know whether these values are dummy
or carry a useful information.
So, in fact, I would like to know if there is a way to use
something sweet like the "in" of other modern languages.
It would be enough if the present "in" would work with array
values instead of only with hash keys.
Or if there was a way to initialise a hash with only keys and
no value (or a default value). For example, with different
delimiters or
with some
special sigil before the hash initialisation, like (the
delimiters and sigil are random, not a real suggestion):
if(myopt in /"opt1", "opt2", "opt3"/) { ... }
if(myopt in $["opt1", "opt2", "opt3"]) { ... }
But perhaps one of these solutions, or some other, already
works and I am just unaware of it. Please enlighten me :-)
Goodbye,
Stéphane.
PS: I tried posting this message to the mailing-list, but I got
"<digitalmars-d-learn@puremagic.com>: delivery to host
puremagic.com[173.45.241.208] timed out". It was the same when I
tried to confirm my subscription to the list, I had to use the
weblink to achieve the confirmation. Does anyone posts through
the mailing list or do you all use the newsgroup or the web forum?
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.
| ||||
February 05, 2015 Re: Syntax for checking if an element exists in a list | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Stéphane | import std.algorithm;
int main(string[] options)
{
// true if the first option given to this program is either foo, bar, or baz.
if(options[1].canFind("foo", "bar", "baz"))
return 0;
return 1;
}
| |||
February 05, 2015 Re: Syntax for checking if an element exists in a list | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Stéphane | 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) | |||
February 05, 2015 Re: Syntax for checking if an element exists in a list | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Nicholas Wilson | On Thursday, 5 February 2015 at 13:31:21 UTC, Nicholas Wilson wrote:
> 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)
addendum
case n:
...
break;
exists only for the ease of transliteration of C(++) where the break statement is required.
How ever
break label;
is still useful and used in D. usually used to break from multiple in a single statement.
| |||
February 05, 2015 Re: Syntax for checking if an element exists in a list | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Nicholas Wilson | On Thursday, 5 February 2015 at 13:31:21 UTC, Nicholas Wilson wrote: > 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(); > } No, that's not right. The docs [1] say that "[a] ScopeStatementList must either be empty, or be ended with a ContinueStatement, BreakStatement, ReturnStatement, GotoStatement, ThrowStatement or assert(0) expression unless this is the last case." That's different from what you said. `case "opt1": doA(); case "4": do_B();` is supposed to be forbidden. It's not supposed to have an implicit break. And apparently, dmd is not there (yet?). Implicit fall-through is only a warning at the moment (which have to be switched on). It's not an error. [1] http://dlang.org/statement.html#switch-statement | |||
February 05, 2015 Re: Syntax for checking if an element exists in a list | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Tobias Pankrath | On Thursday, 5 February 2015 at 12:35:03 UTC, Tobias Pankrath wrote:
> import std.algorithm;
>
> if(options[1].canFind("foo", "bar", "baz"))
This looks quite OK. Thank you, I did not know about that possibility.
| |||
February 05, 2015 Re: Syntax for checking if an element exists in a list | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Nicholas Wilson | On Thursday, 5 February 2015 at 13:31:21 UTC, Nicholas Wilson wrote: > Note that D does NOT have default fall through. i.e. Yes, but I thought I read that we always had to explicitely specify one ending statement, as "goto", "continue"... or "break"; which, in many basic cases, means having to add "break" at the end of each case. One motivation was to force to make it look like C (I cannot say I agree with this purpose, but I think I read it somewhere). >> 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. I was talking about my problem with the mailing-lists (the one in my "PS"); I am not sure you are talking about it. | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply