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

Hi, I have a large array and need to split the array into 10 small arrays. "@accts" is my large array. The splitted arrays should be like : @accts1,@accts2....so on. can anyone please help me with the logic to do so.?

  • Comment on Split a large array into 10 smaller arrays

Replies are listed 'Best First'.
Re: Split a large array into 10 smaller arrays
by Athanasius (Archbishop) on Mar 13, 2013 at 09:47 UTC

    The following script lacks error-checking, but should give an idea of how to proceed:

    #! perl use strict; use warnings; use Data::Dump; my @accts_split; my @accts = (23 .. 257); my $start = 0; my $length = @accts / 10; for my $i (0 .. 9) { my $end = ($i == 9) ? $#accts : $start + $length - 1; @{$accts_split[$i]} = @accts[$start .. $end]; $start += $length; } dd @accts_split;

    Output:

    19:42 >perl 571_SoPW.pl ( [23 .. 45], [46 .. 69], [70 .. 92], [93 .. 116], [117 .. 139], [140 .. 163], [164 .. 186], [187 .. 210], [211 .. 233], [234 .. 257], ) 19:44 >

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Split a large array into 10 smaller arrays
by tobyink (Canon) on Mar 13, 2013 at 10:59 UTC
    use 5.008; use strict; use warnings; use List::MoreUtils qw( part ); use List::AssignRef; use Data::Dumper; my @array = (1 .. 40); my $i = 0; my $j = @array / 10; ( deref(my @array1), deref(my @array2), deref(my @array3), deref(my @array4), deref(my @array5), deref(my @array6), deref(my @array7), deref(my @array8), deref(my @array9), deref(my @array10), ) = part { int($i++/$j) } @array; print "Let's see what's in \@array1... "; print Dumper \@array1; print "Let's see what's in \@array2... "; print Dumper \@array2; print "Let's see what's in \@array10... "; print Dumper \@array10;
    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
Re: Split a large array into 10 smaller arrays
by rnewsham (Curate) on Mar 13, 2013 at 09:56 UTC

    I would recommend looking at why you believe you need to have the data in separate arrays. Can you not simply extract the relevant portion from @accts at the point where you would use one of @acctsN.

    If it really is necessary then here is a basic example. This is not scalable and is not intended as a practical solution it just gives you an idea of how it can be done.

    use strict; use warnings; my @accts = ( 1 .. 10); my @accts1 = @accts[0..1]; my @accts2 = @accts[2..3]; my @accts3 = @accts[4..5]; my @accts4 = @accts[6..7]; my @accts5 = @accts[8..9];

      It works for smaller arrays,but here the case is array is large,the size of array is dynamic,some times it have 10000 elements,some times 15000. Irrespective of size,I need to split the array into 10 parts.

Re: Split a large array into 10 smaller arrays
by BrowserUk (Patriarch) on Mar 13, 2013 at 12:36 UTC

    Using symbolic references:

    @A = 1 .. 100;; @{"a$_"} = @A[ $_*10 .. ($_+1)*10-1 ] for 0 .. $#A/10;; print @{"a$_"} for 0 .. 9;; 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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

    Using an AoAs:

    @B = 1 .. 100;; @{ $subarry[$_] } = @B[ $_*10 .. ($_+1)*10-1 ] for 0 .. $#B/10;; print "@{ $subarry[ $_ ] }" for 0 .. $#B/10;; 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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 pp \@subarry;; [ [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, 26, 27, 28, 29, 30], [31, 32, 33, 34, 35, 36, 37, 38, 39, 40], [41, 42, 43, 44, 45, 46, 47, 48, 49, 50], [51, 52, 53, 54, 55, 56, 57, 58, 59, 60], [61, 62, 63, 64, 65, 66, 67, 68, 69, 70], [71, 72, 73, 74, 75, 76, 77, 78, 79, 80], [81, 82, 83, 84, 85, 86, 87, 88, 89, 90], [91, 92, 93, 94, 95, 96, 97, 98, 99, 100], ]

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Split a large array into 10 smaller arrays
by Anonymous Monk on Apr 05, 2016 at 17:35 UTC

    I came across this looking for a better way than the one I spun. I prefer working with references on this sort of thing, so here is my spin on the AofA approach. This can work with an arbitrary number of arrays thanks to autovivification:

    #!/usr/bin/env perl use strict; use warnings; use Data::Printer; my @original = (1,2,3,4,5,6,7,8,9); my $numberofarrays = 2; my @arrayrefs; while (@original) { foreach (0..$numberofarrays-1){ if (@original) { push @{$arrayrefs[$_]}, shift @original; } } } p @arrayrefs;

    To get at the one you want, simply dereference it:

    print "@{$arrayrefs[0]}\n";

      I just checked list utils, and searched cpan for a package for this sort of thing. I found Array::Split which does exactly what I did - only without chewing through the original with pop. Using this package:

      #!/usr/bin/env perl use strict; use warnings; use Data::Printer; use Array::Split qw( split_into ); my @original = (1,2,3,4,5,6,7,8,9); my $numberofarrays = 2; my @arrayrefs = split_into($numberofarrays,@original);