resulting inuse strict; sub map2 (&@) { use Carp; my $code = shift; croak 'Odd number of values in list' if @_ & 1; map { local ($a, $b) = (shift,shift); $code->() } 1 .. (@_>>1); } my @array = ('A'..'J'); print 'PRINTING map2{ print "$a $b"; } @array; =>'; map2{ print "$a $b"; } @array; print "\n",'PRINTING map2{ print "$a $b"; } qw[A B C D]; =>'; map2{ print "$a $b"; } qw[A B C D E F G H I J];
E:\>228837.pl PRINTING map2{ print "$a $b"; } @array; =>A BC DE FG HI J PRINTING map2{ print "$a $b"; } qw[A B C D]; =>A BC DE FG HI J
resulting in:#!/usr/bin/perl -slw use strict; sub map2 (&@) { use Carp; my $code = shift; croak 'Odd number of values in list' if @_ & 1; map { local ($a, $b) = (shift,shift); $code->() } 1 .. (@_>>1); } my @array = ('A'..'J'); print 'PRINTING map2{ print "$a $b"; } @array; =>'; map2{ print "$a $b"; } @array; print "\n",'PRINTING map2{ print "$a $b"; } qw[A B C D]; =>'; map2{ print "$a $b"; } qw[A B C D E F G H I J];
{sco}>./228837.pl PRINTING map2{ print "$a $b"; } @array; => A B C D E F G H I J PRINTING map2{ print "$a $b"; } qw[A B C D]; => A B C D E F G H I J
Hope this helps.
-enlil
In reply to Re: Problem emulating built-ins like map and grep.
by Enlil
in thread Problem emulating built-ins like map and grep.
by BrowserUk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |