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}
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.