in reply to Can map or splice replace my use of for loop + push + grep for my array transmogrification?

It has just occurred to me that the digit check might only be there because you were using a buggy for loop. If so, the solutions are much simpler.

for:

my @fulls; for my $area (@areas) { for my $short (@shorts) { push @fulls, "$short.area$area"; } }

map:

my @fulls = map { my $area = $_; map "$_.area$area", @shorts } @areas;

More intuitive map, but different order produced:

my @fulls = map { my $short = $_; map "$short.area$_", @areas } @shorts;

NestedLoops:

use Algorithm::Loops qw( NestedLoops ); my @fulls = NestedLoops( [ \@areas, \@shorts ], sub { "$_[1].area$_[0]" }, );
  • Comment on Re: Can map or splice replace my use of for loop + push + grep for my array transmogrification?
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: Can map or splice replace my use of for loop + push + grep for my array transmogrification?
by jffry (Hermit) on Nov 04, 2009 at 00:30 UTC

    Doh! You're right. Sorry about that. In simplifying and sanitizing my example for posting, the digit check no longer makes sense, does it?

    I must do the digit check because that is just one form of shorthand that I'm allowing. The other forms don't match that pattern.