in reply to Re: Array Issues
in thread Array Issues

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.

Replies are listed 'Best First'.
Re: Re: Re: Array Issues
by liz (Monsignor) on Aug 28, 2003 at 14:32 UTC
    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

Re3: Array Issues
by dragonchild (Archbishop) on Aug 28, 2003 at 14:30 UTC
    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.

Re: Re: Re: Array Issues
by antirice (Priest) on Aug 28, 2003 at 14:48 UTC

    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