in reply to Re: Counting multiple values in an array...
in thread Counting multiple values in an array...

That's what you get for not reading.

You stored it in a variable, which is fine sometimes, but something this straightforward, you don't need to, as said below.

You'll find that there can be some performance impacts when storing whole files into variables, mainly RAM I/O.

As said below, the main culprit why your function isn't doing anything is because you've only defined it, not called it. Adding to jwkrahn's code

#!/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"; } }
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.

We've also shown an example of how to define and call a function, and how to pass a hash as a hash reference and then use it in the sub function/procedure.

Update: because we are using a hashref the extra '$' was necessary in calling the value of for the key: $$hashref{$key}