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.