dukea2006 has asked for the wisdom of the Perl Monks concerning the following question:
Hello Monks, I am working on some code that will process through multiple elements in an array and calculate the total number of entires for each field.
The data is in a tab delimited file which I pull into an array and grab all the fields in a foreach loop. Next I want to proceed with a few of the calculations for the defined fields. In the case below, I want to count the total number of $caseNumber entries.
#!/usr/bin/perl use warnings; use strict; # open file my $file = shift @ARGV; open (FILE1, "<", $file) or die "Can't open '$file': $!"; # read file into an array my @data = <FILE1>; # close the file you don't need it any more close (FILE1); # Use a FOREACH loop to read through the data in the array foreach my $caseMetrics (@data) { (my $caseOwner,my $caseNumber,my $accountName,my $contactName, +my $openedDate,my $subject,my $status,my $priority,my $severity,my $a +geHours,my $caseOrigin,my $closedDate,my $highWaterMark,my $accountRe +gion,my $industryConcentratio,my $industry,my $apiType,my $applyFromA +pp,my $environmentAffecting,my $osBackend,my $osFrontend,my $version, + my $maintenanceLevel)=split('\t',$caseMetrics); #Calculate the number of total cases. sub total_cases{ my @closedCases = $caseNumber; my %caseCount; map {$caseCount{$caseNumber}++} @closedCases; return %caseCount; } }
I'm not sure that the sub routine is the way to go here. It doesn't work, nor does it throw an error (which is surprising given my lack of coding prowess). An important note here is that I tried the code as is and by omitting the "my @closedCases" array in the sub so, I think I am way off the rails here. I'm guessing that there is a better way to pull these kinds of counts from the array.
Any suggestions would be most appreciated! Thanks! Duke
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Counting multiple values in an array...
by jwkrahn (Abbot) on Jun 09, 2010 at 20:55 UTC | |
by chromatic (Archbishop) on Jun 10, 2010 at 07:46 UTC | |
by jwkrahn (Abbot) on Jun 10, 2010 at 16:27 UTC | |
|
Re: Counting multiple values in an array...
by toolic (Bishop) on Jun 09, 2010 at 20:52 UTC | |
|
Re: Counting multiple values in an array...
by dukea2006 (Novice) on Jun 10, 2010 at 13:24 UTC | |
|
Re: Counting multiple values in an array...
by deMize (Monk) on Jun 09, 2010 at 20:54 UTC | |
by deMize (Monk) on Jun 09, 2010 at 21:17 UTC |