arun15986:

Your requirements are still unclear (to me, at least), but it looks like you just need to buffer some records. You can store records by putting them in an array, then print them as needed. Here's a simple example program (similar to yours, but I didn't try to meet your requirements):

#!/usr/bin/perl -w use strict; use warnings; my @record_buffer; while (<DATA>) { print "INP (buff=" . scalar(@record_buffer) . "): $_"; my @data = split /\s+/, $_; if ($data[1] < 20) { if (@record_buffer > 3) { print_buffered_recs( "WORSE" ); } elsif (@record_buffer > 0) { print_buffered_recs( "BAD" ); } print_rec( \@data, "GOOD" ); } elsif ($data[2] > 5) { print_buffered_recs( "FAIR" ); print_rec( \@data, "XYZZY" ); } else { # We don't know yet, store the record push @record_buffer, [ @data ]; } } # At the end, you may still have buffered records, so be # sure to do something with them! print_buffered_recs( "EXTRA" ); sub print_buffered_recs { my $comment = shift; print_rec($_, $comment) for @record_buffer; @record_buffer = (); } sub print_rec { my $rRec = shift; my $comment = shift; print "OUT: ", join(", ", @$rRec, $comment), "\n"; } __DATA__ HI 28 0 FUNKY HI 27 1 FUNKY HI 26 3 FUNKY HI 25 2 FUNKY HI 24 3 FUNKY HI 19 6 FUNKY LO 1 11 ODD LO 3 6 ODD LO 4 1 WIERD SEL 5 1 ECCENTRIC SEL 2 1 N/A SEL 2 1 N/A SEL 2 1 N/A SEL 2 7 N/A OUT1 1 1 N/A OUT2 1 1 N/A HI 28 0 N/A HI 28 0 ODD

This gives the output:

[08:21:18] ~ $ ./820888.pl INP (buff=0): HI 28 0 FUNKY INP (buff=1): HI 27 1 FUNKY INP (buff=2): HI 26 3 FUNKY INP (buff=3): HI 25 2 FUNKY INP (buff=4): HI 24 3 FUNKY INP (buff=5): HI 19 6 FUNKY OUT: HI, 28, 0, FUNKY, WORSE OUT: HI, 27, 1, FUNKY, WORSE OUT: HI, 26, 3, FUNKY, WORSE OUT: HI, 25, 2, FUNKY, WORSE OUT: HI, 24, 3, FUNKY, WORSE OUT: HI, 19, 6, FUNKY, GOOD INP (buff=0): LO 1 11 ODD OUT: LO, 1, 11, ODD, GOOD INP (buff=0): LO 3 6 ODD OUT: LO, 3, 6, ODD, GOOD INP (buff=0): LO 4 1 WIERD OUT: LO, 4, 1, WIERD, GOOD INP (buff=0): SEL 5 1 ECCENTRIC OUT: SEL, 5, 1, ECCENTRIC, GOOD INP (buff=0): SEL 2 1 N/A OUT: SEL, 2, 1, N/A, GOOD INP (buff=0): SEL 2 1 N/A OUT: SEL, 2, 1, N/A, GOOD INP (buff=0): SEL 2 1 N/A OUT: SEL, 2, 1, N/A, GOOD INP (buff=0): SEL 2 7 N/A OUT: SEL, 2, 7, N/A, GOOD INP (buff=0): OUT1 1 1 N/A OUT: OUT1, 1, 1, N/A, GOOD INP (buff=0): OUT2 1 1 N/A OUT: OUT2, 1, 1, N/A, GOOD INP (buff=0): HI 28 0 N/A INP (buff=1): HI 28 0 ODD OUT: HI, 28, 0, N/A, EXTRA OUT: HI, 28, 0, ODD, EXTRA [08:22:03] ~ $

...roboticus


In reply to Re^3: Confusing loop:( by roboticus
in thread Confusing loop:( by arun15986

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.