Solved

changing the output of data to the textfile

Posted on 2009-07-12
4
193 Views
Last Modified: 2012-05-07
the code I have below sets out data as shown :

232      356      434      767      786      854      avg 571.50      group 0011100210
124      78      333      19      68            avg 124.40      

how do I change the code to make the output all of the above but on the one line:

232,356,434,767,786,854,avg1,571.50,0011100210124,78,333,19,68,avg2,124.40


#!/usr/bin/perl
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
        while(<$IN>){
        chomp;
        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)x10;
        foreach my $a (@arr) {
                $groups[$a/100]++;
        }
        #   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;
 
 
close($IN);
close($OUT);

Open in new window

0
Comment
Question by:MichaelGlancy
  • 2
  • 2
4 Comments
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24836542
Try this.

The line I changed:

printf $OUT "%s,avg1,%.2f,%s,%s,%.2f\n", join(",", @arr), $avg1, join("", @groups), join(",", @diff), $avg2;
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
        while(<$IN>){
        chomp;
        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)x10;
        foreach my $a (@arr) {
                $groups[$a/100]++;
        }
        #   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
        printf $OUT "%s,avg1,%.2f,%s,%s,%.2f\n", join(",", @arr), $avg1, join("", @groups), join(",", @diff), $avg2;
}
 
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;
 
 
close($IN);
close($OUT);

Open in new window

0
 
LVL 40

Accepted Solution

by:
mrjoltcola earned 500 total points
ID: 24836558
Oops I missed the "avg2" word

printf $OUT "%s,avg1,%.2f,%s,%s,avg2,%.2f\n", join(",", @arr), $avg1, join("", @groups), join(",", @diff), $avg2;
#!/usr/bin/perl
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
        while(<$IN>){
        chomp;
        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)x10;
        foreach my $a (@arr) {
                $groups[$a/100]++;
        }
        #   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
        printf $OUT "%s,avg1,%.2f,%s,%s,avg2,%.2f\n", join(",", @arr), $avg1, join("", @groups), join(",", @diff), $avg2;
}
 
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;
 
 
close($IN);

Open in new window

0
 

Author Closing Comment

by:MichaelGlancy
ID: 31602668
excellent, thank you
0
 

Author Comment

by:MichaelGlancy
ID: 24843541
so would that look like this in code to open data file and write to another file ?

use strict;
use warnings;

 

 
##### Open files

open(my $IN,"outvim2.vim") or die "Could not open input: $!\n";
open(my $OUT,">groupsort.vim") or die "Could not open output: $!\n";


my $Group_Min = 501;
my $Group_Max = 550;
my $Group_Count = 10;
 
my @averages = qw(571.50 551.50 551.17 531.17);
 
my @groups = (0)x$Group_Count;
foreach (@averages) {
        my $group = ($_ - $Group_Min)/$Group_Count;   #This calculates the group
        $group = 0 if $group < 0;    #In case you have a number below the expected min
        $group = $Group_Count-1 if $group >= $Group_Count;    #In case you have a number above the expected max
        $groups[$group]++;
}
 
print OUT join(", ", @groups) . "\n"
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
crawling gofundme 4 132
Perl Scripting from a shell script with if and for 4 87
ppm conversion to curl on a module install 8 86
Regular Expression for URL 10 102
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 …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…

828 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