Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
I am trying to write a program that reads in a tab delimited file of data, containing numerical values e.g a snippet of the file looks like this... p.s in the real file there is three lots of data like this.
All i want to do is calculate the greatest difference between two consecutive numbers in the second column, which i have done in the below script, i then want to return the corresponding temperature values either side of the diffrence e.g. the difference between 9.265 and 9.103 is 0.162 which lies between temperature 65.4 and 75.2. which is the part i am struggling with. I have attached my code to show you what i've done so far.. if anyone can help me with this problem it will be greatly appreciated..TEMP 54.3 65.4 75.2........... 9.3254 9.265 9.103.......... 9.843 9.743 9.421...............
#!/usr/bin/perl -w use strict; my $num_of_params; $num_of_params = @ARGV; if ($num_of_params < 2) { die ("\n You haven't entered enough parameters!!! \n\n"); } # Declare some variables. my $line; my $data; my @derivative; my $value; my $line2; my @array; my $difference; my $number_counter = 0; my $test; my $test2; my $k; my $i; open (EXCEL, $ARGV[0]) or die "unable to open file, perl says $!"; open (OUTFILE, ">$ARGV[1]") or die "unable to open file, perl says $!" +; $data = do { local $/; <EXCEL>; }; $data =~ s/([^\n]) (\n) ([^\n]) /$1$3/g; $data =~ s/\n+/\n/g; my @data = split /\n/, $data; $data[6] =~ s/^1{1}//; $data[7] =~ s/^13{1}//; $data[5] =~ s/TEMP{1}//; my @numbers = split (/\t/, $data[6]); my @numbers2 = split (/\t/, $data[7]); my @temps = split (/\t/, $data[5]); my %differences = (); my %differences2 = (); my $diff = -1; my $diff2 = -1; my $index = 0; my $d; my @d; my %hash; for ($i = 2; $i < @numbers; $i++) { if (defined ($numbers[$i]) && defined ($numbers [$i-1])) { $d = abs ($numbers [$i] - $numbers [$i-1]); $d =~ s/-//g; $diff = $d if $diff < $d; } #### this is the main bit where i'm not sure what i'm doing. ##### I was trying to set up a hash that could look up the correspond +ing ### key value in @temps. #@d = $d; #%hash = map {$temps[$_] => $numbers[$_] => $d[$_] } 0 .. $#temps; } # this just calculates the biggest difference between two consecutive +numbers print "Greatest difference in the first sample is $diff\n"; for (my $k = 2; $k < @numbers2; $k++) { if (defined ($numbers2[$k]) && defined ($numbers2 [$k-1])) { my $e = abs ($numbers2 [$k] - $numbers2 [$k -1]); $e =~ s/-//g; $diff2 = $e if $diff < $e; } } print "Greatest difference in the second sample is $diff2\n"; close EXCEL; close OUTFILE; exit 0; ##******************************************************************** +******
update-ed (broquaint): formatted code by removing excessive blanks lines, added a <readmore> tag + title change (was a mixture of confusion)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: a mixture of confusion
by rdfield (Priest) on Dec 03, 2002 at 14:35 UTC |