in reply to Re: use array for hash-keys without loop
in thread use array for hash-keys without loop

You're asking to do something that's the very definition of looping without using a loop. What do you actually want?

I also sometimes wonder why we regularly get questions how to do things "without a loop"...  but I think what people are typically looking for is just some kind of syntactic shortcut that avoids having to spell out things the cumbersome way. Like, for example in this case:

my @a = qw(foo bar baz); my @b = (1, 42, 99); # unwanted (considered too clumsy): for my $i (0..$#a) { $h{$a[$i]} = $b[$i]; } # or $h{$a[0]} = 1; $h{$a[1]} = 42; $h{$a[2]} = 99; # or ($h{$a[0]}, $h{$a[1]}, $h{$a[2]}) = (1, 42, 99); # wanted: @h{@a} = @b; @h{@a} = (1, 42, 99);

They don't particularly care about the internal implementation, i.e. that something behind the scenes of course will have to do the looping... At least, that's my guess.

Replies are listed 'Best First'.
Re^3: use array for hash-keys without loop
by ikegami (Patriarch) on Mar 11, 2010 at 17:04 UTC

    It's often about perceived performance, I believe.

    Sure, it's a clarity question here. What does that have to do with avoiding loops? I'd take

    $b{$_}=1 for @a;
    over
    @b{@a} = (1)x@a;

    even though the other might be considered to have no loop. (It has three while the first has two.)

    Actually, I'd take the following since it incorporates the my:

    my %b = map { $_ => 1 } @a;