in reply to Re: Re: BASIC MATH WITH DATA
in thread BASIC MATH WITH DATA

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.

Replies are listed 'Best First'.
Re: Re: Re: Re: BASIC MATH WITH DATA
by joachim (Initiate) on Jan 26, 2001 at 04:40 UTC
    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

      If you are comfortable using the debugger, now would be a good time to fire it up: "perl -d script.pl". If you aren't comfortable, then you might consider this as a good time to get comfortable. (: Otherwise, I suggest you get more data. Data::Dumper is great for this, but you could start with just a:

      print "($cols[3]) $_";
      just above the $total += $cols[3]; line.

              - tye (but my friends call me "Tye")
        tye...... thank you for having a look. I can see that you trying to get the data to dump.... I tried your line entry and ..... nothing dumped .... in short the code is running but not doing the calculation. As for data...... I am trying to do the calculation for a 10 entry average.... the test file is 31Kb of data.... more than sufficient for this small program. We should be able to get a 200 event average with that amount of data.
        #!/usr/bin/perl -w use strict; $my infile = shift; $my outfile = shift; open(IN, $infile) || die("Can't open input file $infile - $!"); open(OUT, ">$outfile") || die("Can't open output file $outfile +- $!"); my $text; my $text2; my @lines; while (<IN>) { push @lines; shift @lines if @lines > 10; } chomp(@lines); my @cols; my $total = 0; for (@lines) { @cols = split /:/; print "($cols[3]) $_"; $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"; + $text2 ="Last value in Col3 " . scalar(@lines) . " $last\n"; print OUT "$text\n"; print OUT "$text2\n"; close IN; close OUT ;
        **** this is the best edited version till now.... have a look..... it would be easy to give up in frustration.... it's being able to do caculations like this .... is what scripting is all about... for me anyway. Especially when you can see ways of doing the work faster and simpler than in Excel .... once you get it coded right that is.. Joachim