in reply to Hash multiple initialization

Updated: per ++cdarke's post below.

my %hash; @hash{qw[ a b c f t u] } = ('toto') x 6; @hash{qw[ b g k p ]} = ('titi') x 4;

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

Replies are listed 'Best First'.
Re^2: Hash multiple initialization
by cdarke (Prior) on Oct 08, 2008 at 11:13 UTC
    Wow! It is not often that BrowserUK makes a typo (his code sets just the first key). I think he meant:
    my %hash; @hash{qw[ a b c f t u] } = ('toto') x 6; @hash{qw[ b g k p ]} = ('titi') x 4;
    My personal favorite is:
    my %hash; my @array = qw[ a b c f t u]; @hash{@array } = ('toto') x @array;
    Update:Following a request from blazar: the typos made by BroswerUK were that the parentheses were omitted from the lhs of the x operator in both cases, and the final line was terminated with a comma instead of a semi-colon. It was probably the keyboard at fault - I have one just like it.

    Anyway, I prefer my solution ("My personal favorite") because there is nothing to change when the array changes size.