Update: Now available in several flavors as part of Algorithm::Loops.

runrig was complaining about no mapcar in Perl so I wrote one in the chatterbox but it scrolled off. So here are two better versions.

mapcar is from lisp. It is like map but works on more than one list. While map loops setting $_ to successive elements of a list, mapcar loops setting @_ to successive elements of several lists (but since you can only pass one list to a subroutine, mapcar expects a list of references to one or more arrays). Both map and mapcar collect the values returned by the code block or subroutine and then return the collected values.

# Sample usage: my @array= ( [ 'a'..'c',undef ], [ 1..7 ], [ 'A'..'E' ] ); my @trans= mapcar { [@_] } @array; # @transpose is now ( ['a',1,'A'],['b',2,'B'],['c',3,'C'], # [undef,4,'D'],[5,'E'],[6] ) my @transpose= mapcaru { [@_] } @array; # @trans is now ( ['a',1,'A'],['b',2,'B'],['c',3,'C'], # [undef,4,'D'],[undef,5,'E'],[undef,6,undef] )
So mapcaru puts undefs into @_ when one of the lists is shorter than any of the others while mapcar just leaves values out of @_. Both versions are provided since the output of either cannot be easily converted into the other.

If you find yourself wishing for a special Perl variable that tracks which element of a list you are currently iterating over (in map or for), then you might find that mapcar is useful to you.

#!/usr/bin/perl -w package mapcar; use strict; require Exporter; use vars qw( $VERSION @EXPORT @ISA ); BEGIN { $VERSION= 1.01; @EXPORT= qw( mapcar mapcaru ); @ISA= qw( Exporter ); } sub mapcaru (&@) { my $sub= shift; if( ! @_ ) { require Carp; Carp::croak( "mapcaru: Nothing to map" ); } my $max= 0; for my $av ( @_ ) { if( ! UNIVERSAL::isa( $av, "ARRAY" ) ) { require Carp; Carp::croak( "mapcaru: Not an array reference (", ref($av) ? ref($av) : $av, ")" ); } $max= @$av if $max < @$av; } my @ret; for( my $i= 0; $i < $max; $i++ ) { push @ret, &$sub( map { $_->[$i] } @_ ); } return wantarray ? @ret : \@ret; } sub mapcar (&@) { my $sub= shift; if( ! @_ ) { require Carp; Carp::croak( "mapcar: Nothing to map" ); } my $max= 0; for my $av ( @_ ) { if( ! UNIVERSAL::isa( $av, "ARRAY" ) ) { require Carp; Carp::croak( "mapcar: Not an array reference (", ref($av) ? ref($av) : $av, ")" ); } $max= @$av if $max < @$av; } my @ret; for( my $i= 0; $i < $max; $i++ ) { push @ret, &$sub( map { $i < @$_ ? $_->[$i] : () } @_ ); } return wantarray ? @ret : \@ret; } 1;

In reply to mapcar -- map for more than one list by tye

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.