Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-03-28 17:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found