in reply to Confusing loop:(

Your specifications are unclear, and your code needs much better structure.

Here is my interpretation, with code to help get you started (untested):

use strict; use warnings; my $lastrecord = $rdb->last_record; # Call this only once my $i = 0; # Starting record number ? my $counter = 0; while (my ($recordnumber, $X, $Y, $comments) = $rdb->get_record($i,"X", "Y"); $i = $recordnumber; last if $i > $lastrecord; if ($X < 20){ $wdb->set_record($i,$X,$Y,"Good"); }elsif ($Y > 5){ if (++$counter > 10){ $wdb->set_record($i,$X,$Y,"Worse"); }else{ $wdb->set_record($i,$X,$Y,"bad"); } } }
When do you reset $counter ?

     Theory is when you know something, but it doesn't work.
    Practice is when something works, but you don't know why it works.
    Programmers combine Theory and Practice: Nothing works and they don't know why.         -Anonymous

Replies are listed 'Best First'.
Re^2: Confusing loop:(
by arun15986 (Initiate) on Feb 03, 2010 at 05:38 UTC
    Hi I am sorry about it. I need to write comments to the records only after i know the counter value is greater than 10. The basic problem is i traverse through records to count it.But after counting it, how can I go back and write to those records? I am pasting the algorithm I need to do * If absolute value of X is less than 20 -> Write comments with good * If absolute value of X is 20 or greater o Keep looking until Y is greater than 5 o increment Counter of bad records until 5 is reached o if counter of bad records counted is greater than 10 + Write bad_NOTfixed to comments o If counter of consecutive bad records counted is 10 or less Do another operation ( which I can possibly do)

      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