So this question is purely for learning purposes and curiosity, but can anyone explain how the function below works?
sub mesh (\@\@;\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@) {
my $max = -1;
$max < $#$_ && ( $max = $#$_ ) foreach @_;
map {
my $ix = $_;
map $_->[$ix], @_;
} 0 .. $max;
}
It's from the List::MoreUtils module. I'm using it in one of my applications and I happened to see the source code, and it made me feel like I don't know perl at all! Can anyone explain this craziness? :) Thanks!
I won't cover the prototypes part (mob said he will).
Here's a more readable version - ideally, it should be self explanatory
sub mesh (\@\@;\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@) {
# Calculate the maximum number of elements in each of the array refs
# we were passed:
my $maxLength = 0;
foreach my $array_ref (@_) { # @_ is all arrey refs passed in
if ($maxLength < @$array_ref) {
# we found an array longer than all previous ones
$maxLength = @$array_ref;
}
}
# If you represent the arrays as a matrix:
# ARR1 = [ a1e1, a1e2, .... a1eN],
# ARR2 = [ a2e1, a2e2, .... a2eN],
# ...
# ARR2 = [ aMe1, aMe2, .... aMeN];
# Then, we are simply walking through the matrix;
# each column top to bottom then move on to next column left to right
# (a1e1, a2e1, ... aMe1, a1e2, a2e2, ... aMeN)
my @results;
for (my $index = 0; $index < $maxLength; $index++) { # Iterate over columns
foreach my $array_ref (@_) { # Iterate over per-row cells in each column
push @results, $array_ref->[$index];
}
} ;
}
here's a commented original version
sub mesh (\@\@;\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@) {
# Calculate the largest index in each of the array refs
# @_ is an array of all the input arrayrefs
# $_ will be one of the array refs in a foreach loop
# $#{$X} is the largest index in arrayref X; thus
# $#$_ is the largest index in arrayref $_
my $max = -1;
$max < $#$_ && ( $max = $#$_ ) foreach @_;
# Return a list, obtained by looping
# over every index from 0 to the maximal index of any of the arrays
# Then, for each value of the index ($ix), push into the resulting list
# an element with that index from each of the arrays.
map {
my $ix = $_;
map $_->[$ix], @_;
} 0 .. $max;
}
One of the unusual things in this method is the function signature (prototype)
.
sub mesh (\@\@;\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@\@) {
As @mob and @ikegami wisely noted in the comments,
... It instructs Perl to expect between 2 and 32 named arrays, and to pass them to the function (in @_) as array references. So if you call
mesh @a,@b,@c
, then@_
in mesh is set to(\@a,\@b,\@c)
rather than one "flat" list with all the individual elements of@a, @b, and @c
(mob)
... They technically don't need to be named, just dereferenced. e.g.@$ref
and@{[qw( foo bar )]}
work just as well as@a
. In other words, it has to start with@
(and not be a slice). (ikegami)
In other words, the following 2 calls behave the same
my @a1 = (1);
my @a2 = (2);
sub mesh_prototype(\@\@) { print "$_->[0]\n" }
sub mesh_pass_arrayref() { print "$_->[0]\n" }
mesh_prototype(@a1, @a2);
mesh_pass_arrayref(\@a1, \@a2);
This is done so that you can pass individual arrays (and not arrayrefs) as arguments to functions that will behave like built-ins (e.g. map
/sort
)
To answer Zaid's query as to what happens if 1 or 33 arrays are listed as parameters to call to mesh()
, it will generate a compile time error:
Not enough arguments for main::mesh at mesh.pl line 16, near "@a1)"
Execution of mesh.pl aborted due to compilation errors.
Too many arguments for main::mesh at mesh.pl line 16, near "@a2)"
Execution of mesh.pl aborted due to compilation errors.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With