in reply to Re: Counting multiple values in an array...
in thread Counting multiple values in an array...
Notice that we've defined a main procedure called "main", which isn't necessary, but it is easier to comprehend for those C programmers. This way, there are no global variables.#!/usr/bin/perl use warnings; use strict; main(); sub main{ # open file my $file = shift @ARGV; open FILE1, '<', $file or die "Can't open '$file': $!"; my %caseCount; # Use a while loop to read through the data while ( <FILE1> ) { my $caseNumber = ( split /\t/ )[ 1 ]; $caseCount{ $caseNumber }++; } # close the file you don't need it any more close FILE1; #do something here with values in %caseCount # DoSomething is an example of how to call a function DoSomething(\%caseCount); } sub DoSomething{ my $hashref = shift; foreach my $key (sort keys %{$hashref}){ print "$key - ${$hashref}{$key}\n"; } }
|
|---|