When nesting map{}s inside each other, the inner map{} can not have any idea of what the outer map{}'s value of $_ is, without using a temporary variable, which is terribly inelegant. So I wrote this, which I intend to offer as a sacrifice on the altar of CPAN. Any comments are welcome.
package NestedMap; require Exporter; @ISA = qw(Exporter); @EXPORT = qw(nestedmap); use strict; use warnings; # [POD snipped out] sub nestedmap(&@) { my $f = shift; map { local @NestedMap::stack = ($_, @NestedMap::stack); $f->($_); } @_ }
You might use it thus (from the perldoc):
# show all combinations of (A,B,C) (a,b,c) and (1,2,3) print join("\n", nestedmap { nestedmap { nestedmap { join('',@NestedMap::stack[0..2]) } qw(A B C) } qw(a b c) } qw(1 2 3) );
update: thanks to broquaint for pointing out that the example only works in 5.8.0. To get it to co-operate with 5.6.1, change
nestedmap { ... } list
to
nestedmap sub { ... }, list

Replies are listed 'Best First'.
Re: Nested map
by DrHyde (Prior) on Jun 26, 2003 at 08:58 UTC
    I have now uploaded this to CPAN.