Hi Monks,

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.

TEMP 54.3 65.4 75.2........... 9.3254 9.265 9.103.......... 9.843 9.743 9.421...............
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..
#!/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)


In reply to Calculating the difference of consecutive numbers by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.