Update: LanX had the module (List::MoreUtils) that I was trying to find. Nothing to see here. Move along.
I am not certain that this is what you are looking for, but I seem to remember seeing code to fold arrays together in various ways. Below you will find an example of a simple folding subroutine. This could be made generic enough to be able to handle any number of arrays.
use strict;
use warnings;
use Data::Dumper;
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Indent = 0;
sub array_fold {
my $a1 = shift; my @a1 = @$a1;
my $a2 = shift; my @a2 = @$a2;
my $folder = shift || sub { [ @_ ] };
die "Arrays are not of the same size"
unless scalar( @a1 ) == scalar( @a2 );
my @results;
while( scalar( @a1 ) && scalar( @a2 ) ) {
push @results, $folder->( shift( @a1), shift( @a2 ) );
}
return @results;
}
my @a = qw( a b c d );
my @b = qw( A B C D );
my @results = array_fold( \@a, \@b );
print Dumper( { results => \@results } ), "\n";
my @results2 = array_fold( \@a, \@b, sub { "@_" } );
print Dumper( { results2 => \@results2 } ), "\n";
__END__
$VAR1 = {'results' => [['a','A'],['b','B'],['c','C'],['d','D']]};
$VAR1 = {'results2' => ['a A','b B','c C','d D']};
-
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.