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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
shell script to send sms 2 64
Shell Mail is not working 5 79
Parse DNS log 3 36
AWS CLI - Instances, Volumes & Tagging 2 24
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
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 …

743 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

12 Experts available now in Live!

Get 1:1 Help Now