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
208 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

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.
A short article about problems I had with the new location API and permissions in Marshmallow
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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

707 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