Date: 03 Jul 2001 10:48:08 -0500 From: Ren Maddox Subject: Re: map/hash question, functional style Message-Id: On 3 Jul 2001, mordor@fly.srk.fer.hr wrote: > I have a list of hash keys in a variable like this: > @keys = qw(key1 key2 key3); > > and a list of values: > @vals = (1, 2, 3); > > Now, what is the most elegant way to make a hash from this? I'd > prefer using map and not foreach if at all possible. This is my > current code: @hash{@keys} = @vals; > $i = 0; > %hash = map { $keys[$i++] => $_ } @vals; > > Is there a way to avoid global counter? Well, if you really wanted to loop, you *could* loop over the indices: $hash{$keys[$_]} = $vals[$_] for 0..$#keys; (You could use map the same way, but there's really no point.) > Also, except map and grep, what other functional programming > constructs are present in perl? Can map and grep be generalized to > iterate over more than one array? Both map and grep work on lists, not arrays. In list context, an array produces a list of its elements. There is no reason that multiple arrays cannot be used in that way: map { $_ *= 2 }, @ary1, @ary2, @ary3; -- Ren Maddox ren@tivoli.com