in reply to BASIC MATH WITH DATA

Here's one way to find the most recent 10 day average for column 3:
#!perl -w use strict; my @lines; while (<>) { push @lines, $_; shift @lines if @lines > 10; } chomp(@lines); my @cols; my $total = 0; for (@lines) { @cols = split /:/; $total += $cols[3]; } my $average = $total / (@lines || 1); # if no lines, avoid division by zero print "Average of column 3 for the last ", scalar(@lines), " days: $average\n";
I hope that gets you started!

Update: Have to specify $_ in the call to push. Bad chipmunk! No acorns!

Replies are listed 'Best First'.
Re: Re: BASIC MATH WITH DATA
by joachim (Initiate) on Jan 26, 2001 at 01:42 UTC
    Just a little more Help here..... and I think it will work. I added some stuff to make the script load a file and output a log file. However there is some error in the script that I cannot resolve. Could someone please have a look....?? while we are editing the $text line..... could you also include the value of the most resent value entered in col3
    #!/usr/bin/perl -w $infile = shift; $outfile = shift; open(IN, $infile) || die("Can't open input file $infile - $!"); open(OUT, ">$outfile") || die("Can't open output file $outfile +- $!"); use strict; my $text; my @lines; while (<>) { push @lines; shift @lines if @lines > 10; } chomp(@lines); my @cols; my $total = 0; for (@lines) { @cols = split /:/; $total += $cols[3]; } my $average = $total / (@lines || 1); # if no lines, avoid division by zero ***** error here***** $text= "Average of column 3 for the last " + ;#, scalar(@lines)," days: $average\n"; print OUT "$text\n"; close IN; close OUT ;
    So the message in the Data.log file would read "<Filename> Average is <average> the last was <most resent entry for col3>" Thank you.. this has been good experience..... wish something like this was in the book I bought. joachim
      Quote:
      $text= "Average of column 3 for the last " ; #, scalar (@lines)," days: $average\n";
      Originally, that was a print statement, in which the commas were fine. With a scalar assignment, the commas should be replaced with concatenation:
      $text = "Average of column 3 for the last " . scalar(@lines) . " days: $average\n";
      or with join:
      $text = join '', "Average of column 3 for the last ", scalar(@lines), " days: $average\n";
      or with a single interpolated string, or with sprintf... There's more than one way to do it.
       

      The last line is at @lines[-1]; one way to get the most recent value is: my $last = (split /:/, $lines[-1])[3]; The ( )[3] is called a list slice. It's basically a short way of doing:

      my @tmp = split /:/, $lines[-1]; my $last = $tmp[3];
      Instead of assigning to the array @tmp and then looking up index 3, the list slice just does the index directly on the list. They give the same results, though, so use whichever is clearer.
        OK....This is almost there..... thanks for the <IN> tip.... it looked like a small correction... it's gotta be in there... thanks
        #!/usr/bin/perl -w $infile = shift; $outfile = shift; open(IN, $infile) || die("Can't open input file $infile - $!"); open(OUT, ">$outfile") || die("Can't open output file $outfile +- $!"); use strict; my $text; my @lines; #my @tmp = split /:/, $lines[-1]; these two go together #my $last = $tmp[3]; as an alternate way of doing this + routine # learn this in next attempt while (<IN>) { push @lines; shift @lines if @lines > 10; } chomp(@lines); my @cols; my $total = 0; for (@lines) { @cols = split /:/; $total += $cols[3]; } my $average = $total / (@lines || 1); # if no lines, avoid division by zero my $last = (split /:/, $lines[-1])[3]; $text = "Average of column 3 for the last " . scalar(@lines) . " days: $average\n"; + $last ="Last value in Col3" . scalar(@lines) . " value: $last\n"; print OUT "$text\n"; print OUT "$last\n"; close IN; close OUT ; <\code> The message in the log file reads..... <code> Average of column 3 for the last 0 days: 0 Last value in Col30 value:
        Lights are flashing on my machine.... files are poping on the screen.... this is good.... but what am I missing .... all the values read "0" I have run this script through the Perl Builder debugger and it does not indicate errors...which is too bad. There are obviously still some errors in here. Would someone please have a look.... Thanks Joachim
      You might want to read from your IN file.
      Try <IN> instead of just <>.

      Editorial: Instead of just cut and pasting chipmunk's code, try to understand what it does. Pick up Learning Perl, browse the other book reviews & purchase other worthy recommended books

        OK....This is almost there..... thanks for the "IN" tip.... it looked like a small correction... it's gotta be in there. .... thanks ... only one small problem... it's not doing the "OUTPUT" unfortunately.... please have a look.....
        #!/usr/bin/perl -w $infile = shift; $outfile = shift; open(IN, $infile) || die("Can't open input file $infile - $!"); open(OUT, ">$outfile") || die("Can't open output file $outfile +- $!"); use strict; my $text; my @lines; #my @tmp = split /:/, $lines[-1]; these two go together #my $last = $tmp[3]; as an alternate way of doing this + routine # learn this in next attempt while (<IN>) { push @lines; shift @lines if @lines > 10; } chomp(@lines); my @cols; my $total = 0; for (@lines) { @cols = split /:/; $total += $cols[3]; } my $average = $total / (@lines || 1); # if no lines, avoid division by zero my $last = (split /:/, $lines[-1])[3]; $text = "Average of column 3 for the last " . scalar(@lines) . " days: $average\n"; + $last ="Last value in Col3" . scalar(@lines) . " value: $last\n"; print OUT "$text\n"; print OUT "$last\n"; close IN; close OUT ; <\code> The message in the log file reads..... <code> Average of column 3 for the last 0 days: 0 Last value in Col30 value:
        Lights are flashing on my machine.... files are poping on the screen.... this is good.... but what am I missing .... all the values read "0" or are " " blank I have run this script through the Perl Builder debugger and it does not indicate errors...which is too bad. There are obviously still some errors in here. Would someone please have a look.... Thanks Joachim