tsk1979 has asked for the wisdom of the Perl Monks concerning the following question:

Suppose I have 2 arrays, A and B. I want to divide array A into multiple arrays based on the number of elements in array B. suppose A has 100 elements and B has 8, I would like to divide A into 8 different arrays with the first 7 having 13 elements each and the last array having 9 elements. Suppose A has 5 elements and B has more number of elements, then I want A to be divided onto 5 different strings. Any pointers on that?

Replies are listed 'Best First'.
Re: Dividing an array into multiple parts
by holli (Abbot) on Mar 16, 2006 at 11:47 UTC
    Like this?
    use warnings; use strict; use Data::Dumper; my @a; my @b; @a = (1..100); @b = (1..8); print Dumper ( divarray(\@a, \@b) ); @a = (1..5); @b = (1..8); print Dumper ( divarray(\@a, \@b) ); sub divarray { my @a = @{shift @_}; my @b = @{shift @_}; my @c; my $i = 0; my $b = int(@a/@b)+1; for ( @a ) { push @c, [] if $i == 0 or $i % $b == 0; push @{$c[-1]}, $_; $i++; } return \@c; }
    Thinking about it for a minute, this is an improved version of the function:
    sub divarray { my @a = @{shift @_}; my @b = @{shift @_}; my @c; my $b = int(@a/@b)+1; while ( @a ) { push @c, [splice (@a, 0, $b)]; } return \@c; }


    holli, /regexed monk/
      Thanks! I guess this creates a third array c in which each element is an array composed of the elements of a
Re: Dividing an array into multiple parts
by izut (Chaplain) on Mar 16, 2006 at 11:59 UTC

    Could you supply the code you wrote?

    I wrote this:

    use Data::Dumper; use POSIX; @a = (1..100); @b = (1..8); @new_a = (); $maxelmts = POSIX::ceil(scalar @a / scalar @b); $i = 0; foreach (@b) { push @new_a, [grep { defined } @a[$i..($i+$maxelmts)]]; $i += $maxelmts; } print Dumper \@new_a;

    Igor 'izut' Sutton
    your code, your rules.

Re: Dividing an array into multiple parts
by BrowserUk (Patriarch) on Mar 16, 2006 at 15:17 UTC

    There are some combinations of sizes for which deciding the right split requires a little more information as to your priorities for this project.

    For example, if @a has 100 elements and @b has 24, then you could split that into 20*4 + 4*5 or 25*4 or 20*5.

    Would your priority be for exactly 24 subarrays, at least 24 subarrays or at most 24 subarrays? Either of the latter two are more easily calculated than the first. Which is more appropriate will depend very much upon your application.

    For example, when laying out a table, it's generally more aesthetically pleasing to have mostly longer and a few shorter, than mostly shorter and a few longer.

    Eg. 100/24

    24 x 5 25 x 4 24 x 5(4) 24 x 5(4) 1:## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## 2:## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## 3:## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## 4:## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## 5:## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## 6:## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## 7:## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## 8:## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## 9:## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## 10:## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## 11:## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## 12:## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## 13:## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## 14:## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## 15:## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## + 16:## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## 17:## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## 18:## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## 19:## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## 20:## ## ## ## ## ## ## ## ## ## ## ## ## ## 21: ## ## ## ## ## ## ## ## ## 22: ## ## ## ## ## ## ## ## ## 23: ## ## ## ## ## ## ## ## ## 24: ## ## ## ## ## ## ## ## ## 25: ## ## ## ##

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Dividing an array into multiple parts
by smokemachine (Hermit) on Mar 16, 2006 at 14:17 UTC
    could be this?
    perl -e '$na=@a=(1..100); $nb=@b=(1..8); while(@a){print "\@a".++$j."\ +n";for($i=0;$i<=int $na/$nb; $i++){print ((shift @a)."\n")}}'