Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

While loop again!:-( Please help 1

Status
Not open for further replies.

arun10427

Programmer
Oct 24, 2009
29
US
Hi I am accessing a dbf file with lots of records. This is the portion of code where I am having confusion about!
Code:
while ($i<=$rdb->last_record)
{
	my @data = $rdb->get_record($i,"Speed", "Velocity"
	if($data[2]<20)
		{ 
		      $comment = "Good"; 
             wdb->set_record($i,$data[1],$data[2],$comment);
        }
	if($data[2]>20)
		{
	   [COLOR=red]#PLEASE HELP ME FILL THIS PART[/color red]
		}
This is what I have to do

--> If value of velocity is less than 20
-> Write comments as good and new_speed = speed
-->If value of velocity is 20 or greater
->Keep looking until speed value is greater than 5
->Count number of bad records until speed value 5 is reached
->If number of consecutive bad records counted is greater than 10
-->write bad_not_fixed to comments
->If number of consecutive bad records counted is 10 or less
-->Pull 3 previous consecutive good speed records into before array
-->Pull 3 following consecutive good speed records into after array
-->If neither before or after array are filled, write bad_notfixed to comments
-->If both before and after arrays are filled write bad_fixed to comments

The main reason for my confusion is how do you first finish counting the records(10) and then go back and write? Please help me with this

 
arun

I think the reason people aren't responding is that you seem to be posting roughly the same question over and over, and your requirements aren't really very clear.[ol][li]What is a 'bad record'?[/li][li]How can velocity be > 20 if speed is not also > 20?[/li][li]Where does all the writing of comments get done, and which record(s) should it be written to?[/li][li]How many is 'a lot' of records? 200? 5000? 20 million?[/li][li]How far forward do we have to look to fill the array of following 'good' records?[/li][li]And if we don't know what a bad record is, how can we determine what a good one looks like?[/li][/ol]I'm not trying to give you a hard time here - it's just that we can't help if we can't figure out what you want...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Hi Steve,

1. What is a 'bad record'?
Keep looking until speed value is greater than 5. The records you are looking are bad records( Sorry for bringing in "bad". You can ignore the word bad)
2. How can velocity be > 20 if speed is not also > 20? - Speed and velocity are just names of columns. Consider them as X and Y that are independent
3. Where does all the writing of comments get done, and which record(s) should it be written to?
Comment is a separate column which is empty and $comment writes it to records meeting conditions in that loops
wdb->set_record($i,$data[1],$data[2],$comment);

4. How many is 'a lot' of records? 200? 5000? 20 million?
I am traversing till the end of record. So it doesnt matter
5. How far forward do we have to look to fill the array of following 'good' records?
If number of consecutive "bad" records are less than count 10, they are good:)

Sorry about confusion:(
 
As you read each record, push it onto a stack (array). Each time you encounter a good record, you can check the size of the stack to see if it is 10 or more. Then just pop the items off the stack one at a time and update the records in the DB by key, adding whatever comment you want. Once the stack size is 0, just continue.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
This does kind of what you want, although you will have to tweak it a bit. It uses a hash to simulate your database because I don't have a DB file.
Perl:
use strict;
use warnings;

my (%db, @pending);

for (my $i = 1; $i < 200; $i++) { [red]# 1[/red]
   $db{$i} = [15 + int(rand(20)), int(rand(6))];
}

for my $id (sort {$a <=> $b} keys %db) { [red]# 2[/red]
   push @pending, $id;                   
   writePending() if $db{$id}->[1] == 5;
}

sub writePending() {                     [red]# 3[/red]
   my $comment = (@pending > 10) ? 'bad_not_fixed' : 'bad_fixed';     [red]# 4[/red]
   while (@pending > 0) {
      my $id = pop @pending;
      push @{$db{$id}}, $comment;
   }
}
 
for my $id (sort {$a <=> $b} keys %db) { [red]# 5[/red]
   print join("\t", $id, @{$db{$id}}), "\n" ;
}
[ol][li]Fake up some random data[/li][li]Read through the 'database', pushing the records IDs onto the stack.[/li][li]Subroutine to unwind the stack and update the pending rows.[/li][li]Determine the comment.[/li][li]Just print them out to see what's happened.[/li][/ol]Note that it doesn't process the last block of records, as it can't work out whether they are good or bad...


Hope this helps.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Well, I'm really glad I took the time and trouble to answer this one. NOT!

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top