Geetha Weerasooriya am Mittwoch, 27. September 2006 05:58:
> Hi dear all,

Hi Geetha

We (at least I) have to guess the things you don't describe.
And there is no test data to reproduce the wrong output.

> I have written a script which reads the data lines and when a blank line
> is found all the data up to the previous line is considered as a one set
> and new set is started after the next line. Then I subtracted from one
> element of the last line of that set the same element of the first line
> of data.  When I run this program, the trajectory travel time calculated
> is correct only for the first 3 trajectories and there after it is
> wrong.

What was wrong?

> I can't understand why it gives correct value for first three 
> trajectories. Can someone please help me??
> My data file looks like follows:
>
> 5/1/2004
>  07:06:43
> 10
> 139.6668
> 35.52782
> 21.2
> 32952056
> 5593
> 0.86
> 25603
> 3

I guess your data for one record is on a single line, delimited with commas, 
right?

> The script is as follows:
>
> #!perl/bin/perl ;
> use strict;
> #use warnings;
> my $i;
> my $j;
> my $data;
> my @line;
> my @time_in_seconds;

Possibly this variable is the source of the miscalculation.
It is never reset, but you use it with $time_in_seconds[-1] in the code 
("deliver the last entry of the array"), and at some points the array could 
have more elements (from a passed trajectory) than you expect and are 
correct.

The deeper reason for this is that you declare your variables not at the 
appropriate places. You should declare them in the minimal possible scope 
where used, and (re)initialize them at the appropriate place.

> my $travel_time;
> my $trajectory_travel_time;
> my @vid;
> open (FILE, " data.csv" ) || die " can't open the file";
> open (OUT1, "> travel_time.csv") || die " can't open the file";
> open (OUT2, "> trajectory.csv") || die " can't open the file";
> $i = 0;
> $j=1;
> while (<FILE>) {
>     $i++;
>    $data = $_;

$data is never used.

>    chomp;
>    @line=split /,/;
>    $vid[$i] = $line[2];
>
> if (@line != ()) {

if (@line) {

avoids the warning.

>    $time_in_seconds[$i] = $line[-2];
>                   if  ( $i==1) {
>                              $travel_time= 0;
>                       } else {
>
>   $travel_time = $time_in_seconds[$i] - $time_in_seconds[1];
>                   }
>
>           print OUT1 "$_,$travel_time \n";
>
>         } else {
>             $trajectory_travel_time = $time_in_seconds[-1] -
> $time_in_seconds[1];

Here is maybe the miscalculation ($time_in_seconds[-1]).
Has the array as many elements as you expect?

(btw while answering this question you will see why it is mostly advisable to 
base indexes on 0 and not 1).

>            print OUT2 "$vid[$i-1],Trajectory$j, $trajectory_travel_time
> \n";
>            $j++;
>            $i=0;
>            print OUT1 "\n";
>             }
>         }

*If* I did not guess the error source, you can put test data that reproduces 
the wrong output as follows:

1. Put at the end of the script the test data, something like:

__DATA__

5/1/2004,07:06:43,10,139.6668,35.52782,21.2,32952056,5593,0.186,125603,3
5/1/2004,07:06:43,10,139.6668,35.52782,21.2,32952056,5593,0.986,925622,3

5/1/2004,07:06:43,10,139.6668,35.52782,21.2,32952056,5593,0.86,25603,3
5/1/2004,07:06:43,10,139.6668,35.52782,21.2,32952056,5593,0.186,125603,3

5/1/2004,07:06:43,10,139.6668,35.52782,21.2,32952056,5593,0.286,225603,3
5/1/2004,07:06:43,10,139.6668,35.52782,21.2,32952056,5593,0.386,325603,3
5/1/2004,07:06:43,10,139.6668,35.52782,21.2,32952056,5593,0.486,425603,3


2a. Comment out the "open (FILE...." line)
2b. replace the line "while (<FILE>) {" 
    with "while (<DATA>) {"

> When I turn on the " use warnings " it gives the warning ' Use  of
> uninitialized value in numeric ne(!=) at ..... line 37, <FILE> line..
> Here line 37 is" if (@line != ()) {"

(see comment in the code)

regards

Dani

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to