#!/usr/bin/perl use strict; use Data::Dumper; my %hash = map { my $item = pop @$_; map { $_, $item } @$_ } [qw(HELP ?) => sub { print "help\n"; }], [qw(QUIT EXIT LEAVE) => sub { "exit"; }]; &{%hash->{HELP}}(); &{%hash->{'?'}}(); print Dumper(%hash); 1;
yields
help help $VAR1 = '?'; $VAR2 = sub { "DUMMY" }; $VAR3 = 'HELP'; $VAR4 = $VAR2; $VAR5 = 'EXIT'; $VAR6 = sub { "DUMMY" }; $VAR7 = 'QUIT'; $VAR8 = $VAR6; $VAR9 = 'LEAVE'; $VAR10 = $VAR6;
The [qw xxx],[qw xxx] are the list on which the first (leftmost) map operates.
For each [qw(X Y), anon_sub] the block of the leftmost map is executed and the result appended to the resulting hash.
In this block the last element is fetched in $item (my $item = pop @$_;) and then for each remaing element, which is the key, used as value in the resulting hash
(map { $_, $item } @$_)
In reply to Re: multiple keys in hash
by Tomte
in thread multiple keys in hash
by PetaMem
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |