http://qs321.pair.com?node_id=675343


in reply to optimize percentile counting in hash

You need to wrap your code in <code> and </code> tags. The characters [ and ] are special to perl monks and can't put put as literal text in your post. You should also use <p> and </p> tags around paragraphs.

A general tip is to put

use strict; use warnings;
at the start of every script you write until you know what you are doing. They wil pick up typos and dubious practices.

Assuming I've understood your problem then this does what you want.

my @data = get_data(); # or however you get them my %count; foreach my $datum (@data) { ++$count{$datum}; }; foreach my $item_count ( values %count ) { $item_count *= 100/@data; }
Now %count will have a key/value for each datum and its frequency as a percentage.