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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Confusion - While loop

Status
Not open for further replies.

arun10427

Programmer
Oct 24, 2009
29
US
Hey all,
I have a doubt regarding the structure of my loop.
I have a dbf file which has lot of records and fields. I open the dbf file and set the descriptor with this line.
$wdb->set_record($i,$data[1],$data[2]) - from the .dbf file

File Sample.dbf has structure like this
X Y
1 r
1 3
1 6
5 2
5 3
2 8
2 1
etc..there are millions of records like this.
I need to split the X data into chunks and apply a function for each chunk separately. For eg) I need to apply a function on all values of Y corresponding to 1(X), then 5, then 2 and so on.. Could you please help me with this?
Sorry for posting this again? I did not convey it properly last time.
 
The function I mentioned is to find the difference between the prev value of Y and the current value of Y ( for each chunk X) and put the value in Z.

Any help would be appreciated.
 
What about when the value of Y is "r"?

Can you post the expected output data for the example input data above?

Annihilannic.
 
X Y Z
1 2 2
1 3 1
1 6 3
New chunk 5
5 2 2
5 3 1
New Chunk 2
2 8 8
2 1 -7

Taking initial Y value for each chunk to be 0
It should write a new column named Z

 
#!C:\perl\bin
use Date::Day;
use warnings;
use XBase;
use POSIX;
use Data::Dumper;
my $chunk_old;
my @processor_num;
my @files= <Sample.dbf>;
foreach $file(@files)
{
my $rdb = new XBase($file);
$wfile=$file;
$wfile="sm_c.dbf";
my $wdb=XBase->create("name"=>$wfile,
"field_names" =>["X", "Y","Z"],"field_types"=>["N", "N","N"],
"field_lengths"=>[19, 19, 10],
"field_decimals"=>[0, 0, 11]) || die "didn't work\n";

my $i=0;
while ($i<=$rdb->last_record)
{

my @data1 = $rdb->get_record($i,"X","Y");
# Here is where I have to do the processing
#my $z = doprocessing($data1[1],$data[2]);

$wdb->set_record($i,$data1[1],$data1[2],$z);
$i++;
}

$rdb->close;
$wdb->close;
}

Dis is the code I use to access the dbf file
 
So just use some variables to store the previous values of X and Y, and calculate Z appropriately:

Code:
    [gray][i]# Here is where I have to do the processing[/i][/gray]
    [blue]$previous_x[/blue] = [blue]$data1[/blue][red][[/red][fuchsia]1[/fuchsia][red]][/red] [olive][b]unless[/b][/olive] [url=http://perldoc.perl.org/functions/defined.html][black][b]defined[/b][/black][/url] [blue]$previous_x[/blue][red];[/red]
    [gray][i]# new chunk?[/i][/gray]
    [blue]$previous_y[/blue] = [fuchsia]0[/fuchsia] [olive][b]if[/b][/olive] [red]([/red][blue]$data1[/blue][red][[/red][fuchsia]1[/fuchsia][red]][/red] != [blue]$previous_x[/blue][red])[/red][red];[/red]
    [blue]$z[/blue] = [blue]$data1[/blue][red][[/red][fuchsia]2[/fuchsia][red]][/red] - [blue]$previous_y[/blue][red];[/red]
    [blue]$previous_x[/blue] = [blue]$data1[/blue][red][[/red][fuchsia]1[/fuchsia][red]][/red][red];[/red]
    [blue]$previous_y[/blue] = [blue]$data1[/blue][red][[/red][fuchsia]2[/fuchsia][red]][/red][red];[/red]

Annihilannic.
 
I forgot to include the variable initialisation, before the loop:

Code:
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$previous_x[/blue][red];[/red]
[black][b]my[/b][/black] [blue]$previous_y[/blue] = [fuchsia]0[/fuchsia][red];[/red]

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top