Solved

final total number of times value in column 3 > 15 then total number of times both column 3 & 4 are above 15 perl

Posted on 2011-09-22
3
206 Views
Last Modified: 2012-06-27
I'm using the script below. Initially it was intended to find the total number of times the last column exceeded a value separated in 6 categories. Now there is only one category. The other main difference is I am switching columns 3 & 4 because I have a different file format. The problem is when I changed switch -2, -1 and/or $forecasted $observed in the perl script it didn't seem to output the correct values. A sample input file is attached.

So this is what I want the script to do. 1. Count the total number of times column 3 exceeds a value of 15  and 2. count the total number of time where both column 3 & 4 in the same row exceed 15.

sample new script with my attempt at the correct solution

use strict;

my @observed;
my @same;
while( <> ) {
   chomp;
   my($forecasted, $observed) = (split)[-1,-2];
   my $observedCategory = valueToCategory($observed);
   my $forecastedCategory = valueToCategory($forecasted);
   $observed[ $observedCategory ]++;
   $same[ $observedCategory ]++ if $observedCategory == $forecastedCategory;
}

foreach my $category(1) {
   my $observed   = $observed[$category]  || 0;
   my $same       = $same[$category]      || 0;
   printf("$category %3d %3d %5.1f\n", $observed, $same, $observed ? ($same*100)/$observed : 0);
}


sub valueToCategory {
   my($value) = @_;

   my $category;
   if( $value > 15 )    { $category = 2 }
   

   return $category;
}

Open in new window





output file format

1 123 133
2 13  2
3 0 0
4 0 0
5 0 0
6 0 0

new output file format
2  14 4
so4pap.txt
0
Comment
Question by:libertyforall2
  • 2
3 Comments
 
LVL 9

Accepted Solution

by:
oheil earned 500 total points
ID: 36585739
Here is my proposal:

use strict;

my @observed;
my @same;
while( <> ) {
   chomp;
   my($forecasted, $observed) = (split)[-2,-1];      #this line changed
   my $observedCategory = valueToCategory($observed);
   my $forecastedCategory = valueToCategory($forecasted);
   $observed[ $observedCategory ]++;
   $same[ $observedCategory ]++ if $observedCategory == $forecastedCategory;
}

foreach my $category( (1,2) ) {         #this line changed
   my $observed   = $observed[$category]  || 0;
   my $same       = $same[$category]      || 0;
   printf("$category %3d %3d %5.1f\n", $observed, $same, $observed ? ($same*100)/$observed : 0);
}


sub valueToCategory {
   my($value) = @_;

   my $category = 1;       #this line changed
   if( $value > 15 )    { $category = 2 }


   return $category;
}

Open in new window



Using your example data, the output is:
1 206 172  83.5
2  33   4  12.1

which means:
category 1 is forecasted <= 15 : 206 in total,  172 are forecasted <= 15 but observed > 15
category 2 is forecasted > 15 : 33 in total,  4 are forecasted > 15 and observed > 15

Thats what you needed?

Oli



0
 
LVL 9

Assisted Solution

by:oheil
oheil earned 500 total points
ID: 36585744
Sorry, this :

category 1 is forecasted <= 15 : 206 in total,  172 are forecasted <= 15 but observed > 15

is wrong.

True is:

category 1 is forecasted <= 15 : 206 in total,  172 are forecasted <= 15 and observed <= 15

Oli
0
 

Author Closing Comment

by:libertyforall2
ID: 36898435
Great!
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

809 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