Matteo Riva wrote:
Hello everybody, I have two lists and I want to merge them like this:
element 1 of list A, element 1 of list B, A2, B2, A3, B3, etc.
I need this to create a sequence of key/value for an anonymous hash.
I'm using this code:
# @fields contains the names of the hash keys
# $val is a string containing a colon-separated sequence of vales to
# be associated with the keys
my $count = 0;
$data{$key} = { map { $fields[$count++] => $_ } split (/:/, $val) };
which uses a counter variable. I was wondering it there's a more
elegant (AKA perlish) way to mix two lists in this way.
You can use a hash slice.
@{ $data{$key} }{ @fields } = split /:/, $val;
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/