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

Hi All,
I am looking for the syntax which would allow me to utilize one array over and over again for a block of input. I want to match a line, put it and all following lines into the array until a condition is met, probably another pattern. Process the array, print its contents, empty the array, and then put the next chunk of input which matches the same patterns into the same array until the input file is exhausted. Also, if anyone knows a good resource for array manipulation, send it my way as well. This is similar to a previous post see Input to String OR Array. But I think that solution may be more complex than I need it to be. Many thanks in advance, Joe

Replies are listed 'Best First'.
Re: Simple Array Followup
by Perl Mouse (Chaplain) on Oct 27, 2005 at 15:29 UTC
    I guess you want something like:
    my (@array, $keep); while (<$handle>) { if (!@array) {$keep = /FIRST_PATTERN/;} next unless $keep; # Next if we haven't seen the first pattern. push @array, $_; next unless $keep = !/SECOND_PATTERN/; # Next unless the second pattern is seen. process_array \@array; # Process and print print @array; @array = (); }
    As for 'array manipulation', what kind of manipulation are you looking for? What is it what you want that splice doesn't do for you?
    Perl --((8:>*
      That works very well, but what if the first pattern I am using is the same as the second pattern. For eample, a leading number? Does that mkae sense?

        If the patterns are the same, then you actually have a simpler case (though the more complicated one shouldn't break, and is more generalized):

        my @array; while (<DATA>) { last if /^d/ } #discard until the first match while (<DATA>) { if ( @array and /^d/ ) { process_array( @array ); undef @array; next; } push @array, $_; }

        Of course, depending on the size of your data set, you could slurp and split:

        my $data = join('',<DATA>); my @chunk = split(/^\d/m, $data); foreach (@chunk) { process_array( split(/\n/s, $_) ) }

        But this latter assumes your data starts and ends OK (won't skip headers or the like).

        <-radiant.matrix->
        A collection of thoughts and links from the minds of geeks
        The Code that can be seen is not the true Code
        "In any sufficiently large group of people, most are idiots" - Kaa's Law
Re: Simple Array Followup
by radiantmatrix (Parson) on Oct 27, 2005 at 16:52 UTC

    This sounds like it might at least be related to token parsing, in which case Parse::Token might work well for you. Otherwise, it's essentially a simple switch.

    my @array; my $start_regex = qr/^\d{3}:/; # a line beginning with, e.g. 001: my $end_regex = qr/^:::$/; # a line with only ':::' while (<DATA>) { # replace with your FH if ( @array and $_ =~ $end_regex ) { # reached an 'end'. I assume you don't want to keep the end ' +tag' do_something_to(@array); undef @array; } elsif ( @array ) { # filling array push @array, $_; } elsif ( $_ =~ $start_regex and !eof DATA) { # begin filling up the array! push @array, $_; # I assume you want to keep the start 'tag' } else { # we haven't gotten a 'start', } } #_ while{} # deal with the possibility that we 'started' but didn't 'end' if (@array) { warn 'Start with out end at EOF!' }
    <-radiant.matrix->
    A collection of thoughts and links from the minds of geeks
    The Code that can be seen is not the true Code
    "In any sufficiently large group of people, most are idiots" - Kaa's Law