I understood, that you have two arrays holding the start- and end-indices respectively of several ranges ($beginingarray[i] .. $endingarray[i]) to be copied or extracted from the bigger array. So, maybe you only need copies of array-slices (see perldata)? As Corion suggested, use splice to extract ranges if you need to consume from the @bigarray.
Maybe it's a more convenient approach to encode your ranges as list of strings (e.g. @range = qw(20-30 1-5);) or as Array-of-Array(refs) (e.g. @range = ( [20,30], [1,5] );) rather than having separate arrays of start- and end-indices?

use strict; # ranges: 1-5, 20-30, 8-15 my @beginningarray = (1, 20, 8); my @endingarray = (5, 30, 15); #Populate: I.E. Atom#X(Magn.Y): A#0(M0), A#1(M0), ... A#30(M3) my @bigarray = map { "A\#$_(M" . int($_/8) . ")" } 0..30; # copy: leave @bigarray untouched sub copy_from_array { #start-idx, end-idx, from-array_ref my ($startidx, $endidx, $a_ref) = @_; # suggestion: add range checks return @{$a_ref}[$startidx .. $endidx]; } # extract: modify original @bigarray sub extract_from_array { #start-idx, end-idx, from-array_ref my ($startidx, $endidx, $a_ref) = @_; # suggestion: add range checks return splice @{$a_ref}, $startidx, $endidx - $startidx +1; } # example: copy and print ranges for ( my $i = 0; $i < @beginningarray; $i++ ) { my ($from, $to) = ($beginningarray[$i], $endingarray[$i]); print "copy: $from-$to:\n\t"; print join(", ", copy_from_array($from, $to, \@bigarray)), "\n"; } # When using the extraction-method, you need to reverse sort # and check for overlapping ranges! I introduced another # notation to get rid of C-ish for-loops later. # Reverse-sort intervals by start-index (no overlap-check): my @ranges = sort { $b->[0] <=> $a->[0] } ([1,5], [20,30], [8,15]); # example: extract and print ranges foreach (@ranges) { my ($from, $to) = ($_->[0],$_->[1]); print "extract: $from-$to:\n\t"; print join(", ", extract_from_array($from, $to, \@bigarray)), "\n"; } print "the rest:\n\t", join(", ", @bigarray), "\n"; # A#0(M0), A#6(M0), A#7(M0), A#16(M2), A#17(M2), A#18(M2), A#19(M2)

In reply to Re: filtering and dividing an array by Perlbotics
in thread filtering and dividing an array by fraizerangus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.