0

Hi

This may seem pity stupid but I am somehow stuck in this:
I have got a file containing years like this :

1913
1913
1917
1917
1917
1917
1917
1955
1955

now this is just a part of a file containing almost 50000 years.

What I want to do is :
count how many times a single year appears for the above sample, so in this case my output should have been something like this:
year count
1913 2
1917 5
1955 2

I have written a code but since it' not working it's pointless to post it here. still can anybody help me out with this.

Thanks
Aj

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by ajay_p5
0
#!/usr/bin/perl
use strict;
use warnings;
my %years; #Hash to save year as key, count as value
while (<DATA>){
    chomp;
    $years{$_}++;
}
printf "%-10s %s\n",'year', 'count';
foreach (sort keys %years){
    printf "%-10s %d\n",$_, $years{$_};
}
__DATA__
1913
1913
1917
1917
1917
1917
1917
1955
1955
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.