Geetha Weerasooriya wrote:
> Hi dear all,

Hello,

> 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. 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

There are no commas in that "data file" but your code suggests that there
should be.


> The script is as follows:
>  
> #!perl/bin/perl ;
> use strict;
> #use warnings;

Don't comment out strict or warnings, they are very helpful to find mistakes
in your code.


> my $i;
> my $j;
> my $data;
> my @line;
> my @time_in_seconds;
> 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";

You should include the file name in the error message so you know which file
couldn't be opened and you should include the $! variable so you know *why*
the file couldn't be opened.

open( FILE, "< data.csv" )        || die " can't open 'data.csv' $!";
open( OUT1, "> travel_time.csv" ) || die " can't open 'travel_time.csv' $!";
open( OUT2, "> trajectory.csv" )  || die " can't open 'trajectory.csv' $!";


> $i = 0;
> $j=1;

You can delare and define the variables at the same time:

my $i = 0;
my $j = 1;


> while (<FILE>) {
>     $i++;
>     $data = $_;

$data is not used anywhere?


>     chomp;
>     @line=split /,/;

You should declare @line here instead of in file scope:

      my @line = split /,/;


>     $vid[$i] = $line[2];

You are assigning to @vid starting at $vid[ 1 ] but perl's arrays start at
zero.  You probably want to use push instead:

      push @vid, $line[ 2 ];


>     if (@line != ()) {

!= is a numerical comparison operator.  @line is a number (the number of
elements in the array) but () is not.

      if ( @line != 0 ) {


>         $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];
>         print OUT2 "$vid[$i-1],Trajectory$j, $trajectory_travel_time\n";
>         $j++;
>         $i=0;
>         print OUT1 "\n";
>     }
> }
>  
> 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 != ()) {"

Which is why warnings are useful.  It is better to actually fix what is
causing the warning than to turn the warnings off.

It would be more helpful if you could provide some actual data and the
expected output so we could test the code.



John
-- 
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order.       -- Larry Wall

-- 
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