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


In reply to Counting multiple values in an array... by dukea2006

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.