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 5944061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:29:02+00:00 2026-05-22T16:29:02+00:00

How would I split a Perl array of arbitrary size in to a variable

  • 0

How would I split a Perl array of arbitrary size in to a variable number of smaller arrays with the number of elements in each smaller array being distibuted as evenly possible? The original array must not be destroyed.

  • 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-05-22T16:29:03+00:00Added an answer on May 22, 2026 at 4:29 pm

    Off the top of my head:

    use strict;
    use warnings;
    
    use Data::Dumper; # for debugging only 
    
    print Dumper(distribute(7, [1..30]));
    
    # takes number+arrayref, returns ref to array of arrays
    sub distribute {
        my ($n, $array) = @_;
    
        my @parts;
        my $i = 0;
        foreach my $elem (@$array) {
            push @{ $parts[$i++ % $n] }, $elem;
        };
        return \@parts;
    };
    

    This guarantees that number of elements in @parts may only differ by one. There’s anonther solution that would count the numbers beforehand and use splicing:

    push @parts, [ @$array[$offset..$offset+$chunk] ];
    $offset += chunk;
    # alter $chunk if needed. 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How would I split a HTML formatted file into several HTML files (complete with
I am writing many files to disk, and I would like to split them
How would one write a regular expression to use in Python to split paragraphs?
What regex pattern would need I to pass to java.lang.String.split() to split a String
I have a large source repository split across multiple projects. I would like to
I was noticing some curious behavior with Perl's split command, particularly in cases when
I would like to use Perl to take a previously generated SPSS syntax file
I have written a small Perl script and now I would like to create
I would like to split a line such as: name1=value1,name2=value2, .....,namen=valuen two produce two
I have done some basic Perl coding but never something in python. I would

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.