If I understood correctly, you want the indices of the first EVENT A and the first EVENT B after the EVENT A. It would be helpful if you provided an example of desired output for your data.

As for the solution, how about a simple dispatch table?

use strict; use warnings; use feature 'say'; # Sample data my @events = ( 'EVENT A', 'EVENT A', 'EVENT B', 'EVENT A', 'EVENT A', 'EVENT A', 'EVENT B', 'EVENT A', 'EVENT B' ); my ($first_A, $first_B); my $state = 'find_A'; my %parse = ( find_A => sub { my ($event, $index) = @_; if ($event eq 'EVENT A') { $first_A = $index; $state = 'find_B'; } }, find_B => sub { my ($event, $index) = @_; if ($event eq 'EVENT B') { $first_B = $index; $state = 'finished'; } } ); while (my ($index, $event) = each @events and $state ne 'finished') { $parse{$state}->($event, $index); } say 'First occurrence of A: ', $first_A // 'none'; say 'First occurrence of B: ', $first_B // 'none';

The script is going to execute one of the functions from the %parse hash, according to the search state. If an EVENT A still hasn't been found, it will check for 'EVENT A' and save the index if it's found, then it will switch to the second state, searching for 'EVENT B'. After an EVENT B is found, the loop will terminate.


In reply to Re: processing a list of events by kroach
in thread processing a list of events by BluePerlDev

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.