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
203 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

863 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

24 Experts available now in Live!

Get 1:1 Help Now