alter code to print only average and group

Posted on 2009-07-12
Last Modified: 2012-05-07
I have tried changing this code to print to file only the average and the grouping to file, like this


could you show me how to do this please ?

I know it is altering the print OUT lines but what I am doing isnt working
and keep the time stamp at the end
use POSIX;
use strict;
use warnings;
use Data::Dumper;
use List::Util qw(sum);
my $startTime = time();
##### Open files
open(my $IN,"invim2.vim") or die "Could not open input: $!\n";
open(my $OUT,">outvim2.vim") or die "Could not open output: $!\n";
my @avgs1;
my @avgs2;
my ($maxavg1, $minavg1, $maxavg2, $minavg2);
        ##### Step 1: read a line, and split it on comma
        my @arr = split /,/;
        next unless @arr;
        # discard 1st and 2nd last and last item
        # shift @arr;
        # pop @arr;
        # pop @arr;
        # pop @arr;
        ##### Step 2: Sort data into ascending order
        @arr = sort {$a <=> $b} @arr;
        # print "arr @arr\n";
        ##### Step 3: Calculate difference between neighboring pairs
        my @diff = (map {$arr[$_] - $arr[$_-1]} (1..$#arr));
        #  print "diff @diff\n";
        ##### Step 4: Calculate amount in each group
        my @groups = (0)x5;
        foreach my $a (@arr) {
        #   print "group @groups\n";
        ##### Step 5: Calculate average
        my $avg1 = sum(@arr)/@arr;
        # printf "arr avg %.2f\n", $avg1;
        if(!$maxavg1 || $avg1 > $maxavg1) { $maxavg1 = $avg1; }
        if(!$minavg1 || $avg1 < $minavg1) { $minavg1 = $avg1; }
        push @avgs1, $avg1;
        my $avg2 = sum(@diff)/@diff;
        # printf "diff avg %.2f\n", $avg2;
        if(!$maxavg2 || $avg2 > $maxavg2) { $maxavg2 = $avg2; }
        if(!$minavg2 || $avg2 < $minavg2) { $minavg2 = $avg2; }
        push @avgs2, $avg2;
        ##### Step 6: Print results
        print $OUT join("\t", @arr) . "\tavg " . sprintf("%.2f\t", $avg1) . "group " . join("", @groups) . "\n";
        print $OUT join("\t", @diff) . "\t\tavg " . sprintf("%.2f\t", $avg2) . "\n";
        print $OUT "\n";
my $stopTime = time();
my $totalTime = $stopTime - $startTime;
#  printf "input: maxavg %d minavg %d avgavg %d\n", $maxavg1, $minavg1, sum(@avgs1)/@avgs1;
printf $OUT "For data values:\t\t avg avg %.2f\t maximum avg %.2f\t minimum avg %.2f \n",
                        sum(@avgs1)/@avgs1, $maxavg1, $minavg1 ;
#  printf "diff: maxavg %d minavg %d avgavg %d\n", $maxavg2, $minavg2, sum(@avgs2)/@avgs2;
printf $OUT "For data difference values:\t avg avg %.2f\t maximum avg %.2f\t minimum avg %.2f \n",
                        sum(@avgs2)/@avgs2, $maxavg2, $minavg2, ;
printf "Time started: %s   Time ended: %s   Total time:  %d minutes\n",
        strftime("%H:%M", localtime($startTime)), strftime("%H:%M", localtime($stopTime)), $totalTime/60;
printf $OUT "Time started: %s   Time ended: %s   Total time:  %d minutes\n",
        strftime("%H:%M", localtime($startTime)), strftime("%H:%M", localtime($stopTime)), $totalTime/60;

Open in new window

Question by:MichaelGlancy
  • 4
  • 3
LVL 40

Expert Comment

ID: 24836449
In which step?
LVL 40

Accepted Solution

mrjoltcola earned 500 total points
ID: 24836452
Something like this should do:
printf $OUT "%.2f,%s\n", $avg1, join("", @groups);

Open in new window


Author Comment

ID: 24836457
at the print out step 6
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.


Author Closing Comment

ID: 31602661
LVL 40

Expert Comment

ID: 24836474
Remember printf is a special routine that comes in handy

printf <format string>, arg1, arg2, ..., argN

It correlates to the C version of printf, so see: perldoc -f printf
or see here for format specifiers. C/C++/Perl all share pretty much the same format since it originated from C.

I like it because its easy to format the output in the format string rather than concatenating a lot of stuff together with regular print.

Author Comment

ID: 24836480
ok well I can see some of that point

I have noticed

printf <....
printf OUT

the first is printing to the screen ? and the second to the text file ?
LVL 40

Expert Comment

ID: 24836484
Yes. If the first argument to print or printf has no comma separating it and the next argument, then the argument is assumed to be a file stream.

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now