This is easy enough if you work it out without the complication of getting fancy with a one-liner. And I am not sure a "look ahead*" is useful or just complicates what you wish to do.Algorithmically, there are 2 ways to approach this.

1. scan array to the end and mark all indexes where there is a 5; then easily calculate the "range" - don't forget these arrays are 0 indexed

To show what I mean, just scan and collect the indexes where the value is 5:

my @fives_idx = (); my $i = 0; foreach my $e (@lines) { if (5 == $e) { push @fives_idx, $i; } ++$i; }

Once you have the indexes that contain 5, you can apply the concept of array slices to extract the values you're wanting. Note, this is not the same as splice, which can be destructive on your arrays (but potentially useful here nonetheless).

2. scan and store previous elements along the way - there are various ways to do this I can think of that don't require book keeping variables - no example here, but you could use a hash of array references to "scoop" up elementes leading up to each 5, for example.

* Update: I think I know why you want to "look ahead". As it was pointed out by Corion, don't use pop. Not only is this destructive to the array, but it also screws up your view of the list. pop takes the last element of the array, so there is no looking ahead - only behind. If you wish to destroy the array as you process it, use shift. But be warned, while pop doesn't re-index elements (but affects greatest index), shift will cause all elements' indexes to decrease by 1. So, don't use a destructive operation (pop, shift. splice) to iterate or partition your array until you have it working non-destructively and understand why it is working.


In reply to Re: Manipulating Array Indexes by perlfan
in thread Manipulating Array Indexes by TJ777

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.