Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Frequency Calculator

Status
Not open for further replies.

biobrain

MIS
Jun 21, 2007
90
0
0
GB
I have versy simple data like

1
2
5
6
2
8
9
10
1
3
200
50
3
2
1


Out of these I want to list each unique value and its frequency i.e how many time it is in the data

e.g

2 is found 3 time
50 is found 1 time
200 is found 1 time
 
Code:
my @input = <DATA>;
chomp @input;

# count instances of each item
my %dupes = ();
foreach (@input) {
   $dupes{$_}++;
}

# sort them by no. of duplicates descending and print
foreach my $dup (sort { $dupes{$b} <=> $dupes{$a} } keys %dupes) {
   print "$dup was found $dupes{$dup} times\n";
}

__DATA__
1
2
5
6
2
8
9
10
1
3
200
50
3
2
1

Code:
C:\Documents and Settings\Kirsle\My Documents>perl test.pl
2 was found 3 times
1 was found 3 times
3 was found 2 times
6 was found 1 times
50 was found 1 times
9 was found 1 times
8 was found 1 times
200 was found 1 times
10 was found 1 times
5 was found 1 times

-------------
Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
What have you tried so far?

A man after my own heart. ;)

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
The code by Kirsle (Programmer) works perfectly
for me. Thanks a lot.

Thanks a lot for all others as well specially for KevinADC as I found him very helpful all the time.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top