in reply to Re: Simple regex to remove all but a list of words
in thread Simple regex to remove all but a list of words

my %whitelist; @whitelist{@good_words} = (1) x @good_words;
my %whitelist = map { $_ => 1 } @good_words;

Is a better idiom (IMO, of course), because I hate having to use a declaration followed by an initialisation.


Unless I state otherwise, all my code runs with strict and warnings

Replies are listed 'Best First'.
Re^3: Simple regex to remove all but a list of words
by moritz (Cardinal) on Jun 04, 2008 at 22:16 UTC
    So we can start the battle of style: would you rather have a clumsy, useless loop (or map), or rather two clumsy, lines, one with declaration, one with initialization?

    Perl 6 to the help, if you don't mind wrapping your head around function style a bit, you can use the zip operator Z:

    pugs> my @good = <a b c> ("a", "b", "c") pugs> @good Z 1 xx @good (("a", 1), ("b", 1), ("c", 1))

    Pugs doesn't flatten the list correctly, so you can't assign it to a hash in pugs, but a correct implementation will allow that. (Notice also the beauty of not needing any grouping parenthesis, because the precedence levels are just right(tm)).

    # Perl 6: my %hash = @good Z 1 xx @good