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

All,
I have just joined your site, but have been browsing and using code snippets for a while. I have a quick array question.

I have an array with x number of elements (i.e. 64 elements)

Ergo, @array (contains 64 elements)

What I want to do is run through the array from the beginning and place the first 15 elements into a new array, then the next fifteen into a new array, and so on and so forth, so I am left with

@array1 [0..14] @array2 [15..29] @array3 [30..44] @array4 [45..59] @array5 [60..63]
So the 64 elements have been split into new arrays.

I am sure this is do-able, I am not sure how. I have only been coding in Perl for about 8 months, so there is LOTS I don't know

Thanks :-)

update (broquaint): fixed code section and added formatting

Replies are listed 'Best First'.
Re: Array Issues
by dragonchild (Archbishop) on Aug 28, 2003 at 13:50 UTC
    What's wrong with the code you have? Just assign each array slice to a new array and you're good.

    ------
    We are the carpenters and bricklayers of the Information Age.

    The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: Array Issues
by Abigail-II (Bishop) on Aug 28, 2003 at 13:52 UTC
    my @array1 = @array [ 0 .. 14]; my @array2 = @array [15 .. 29]; my @array3 = @array [30 .. 44]; my @array4 = @array [45 .. 59]; my @array5 = @array [60 .. 63];

    Abigail

      Thanks for the quick responses :-) My problem is, the number 64 may change, it is not constant. So I want to be able to make the arrays on the fly with 15 elements in each array. For instance, there could only be three item, therefore just one array would be needed, and it would have 0..2. Thanks.
        I bet that if the number 64 may change, the number of "15 element arrays" will also change. What you need is an array of array (references).
        my @newarray; while (my @array = splice @oldarray,0,15) { push @newarray,\@array; }
        You wind up with an array "@newarray" in which each element contains a reference to an array of at most 15 elements.

        Hope this helps.

        Liz

        use constant NUM_ELEMENTS => 15; # Get the starting_array from somewhere my @starting_array = populate_somehow_with_data(); my $start_index = 0; while ($start_index <= $#starting_array) { my $end_index = $start_index + NUM_ELEMENTS - 1; $end_index = $#starting_array if $end_index > $#starting_array; my @array_slice = @starting_array[$start_index .. $end_index; $start_index += NUM_ELEMENTS; # use @array_slice here to do what you need to do. }

        ------
        We are the carpenters and bricklayers of the Information Age.

        The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

        Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

        Well...you could do this:

        my @arrays = map([ @inarray[$_*15 .. ($_*15+14<$#inarray?$_+14:$#inarr +ay)] ],(0..int($#inarray/15)));

        This will return a list of lists of your @inarray. Check out map, perllol and perldsc for more information about complex data structures in perl. Of course, if you're merely looking to iterate over the data, there are much better ways to do this than taking up more memory than you need by creating copies of all your data.

        Hope this helps.

        antirice    
        The first rule of Perl club is - use Perl
        The
        ith rule of Perl club is - follow rule i - 1 for i > 1

Re: Array Issues
by gjb (Vicar) on Aug 28, 2003 at 14:29 UTC

    It might be nicer later on to have an array of arrays, to do this, simply use:

    my @arrays; $arrays[0] = [@array[0..14]]; $arrays[1] = [@array[15..29]]; ...
    To access element 17, you'd use $arrays[1]->[2]

    You might want to look at the Array::Dissect module which will do this for you.

    Of course, it's easy to do it yourself, see the code below.

    Hope this helps, -gjb-

Re: Array Issues
by Zaxo (Archbishop) on Aug 28, 2003 at 15:23 UTC

    Here is a convenient way to make the suggested AoA independent of how many elements you have in the original, push @aoa, [splice @array, 0, $width] while @array; That will empty @array as @aoa is filled. If $width may vary on the fly, replace that variable with a sub call which can figure out the desired width.

    After Compline,
    Zaxo

Re: Array Issues
by kesterkester (Hermit) on Aug 28, 2003 at 14:34 UTC
    This following will make an array of new arrays for you; I couldn't figure out how to get them into separate arrays, but this is close to what you want.

    The two lines after the foreach loop are to put the last few elements of the original array into its own subarray.

    my @new_arrays; my $subarray_size = 15; my $array_number = int ( scalar @array / $subarray_size ); foreach ( 0..$array_number-1 ) { my @indices = $_*$subarray_size .. ($_+1)*$subarray_size; push @new_arrays, [ @array[@indices] ]; } my @indices = $array_number * $subarray_size + 1 .. scalar @array - 1; push @new_arrays, [ @array[@indices] ];