Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8234155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:22:56+00:00 2026-06-07T18:22:56+00:00

So this question is purely for learning purposes and curiosity, but can anyone explain

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T18:22:58+00:00Added an answer on June 7, 2026 at 6:22 pm

    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.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is purely out of curiosity. I am off school for the summer,
This is a question purely to satisfy my own curiosity. Here in Norway it's
A question purely for curiosity's sake. This is obviously invalid syntax: foo = {}
This is a purely theoretical 'what if' question, so no pressure to rush to
I'm asking this question purely from an ease-of-development standpoint. Performance is not a consideration,
A Purely theoretical question on Tcl. Following this question I was thinking on what
Disclaimer: this question is purely informational and does not represent an actual problem I'm
First of all, this question is purely theoretical. Fact is, whether it's possible or
Disclaimer: This question is meant to be purely theoretical, so please don't ask me
I already solved this question, this Q&A is purely for future Googlers since it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.