#! perl use strict; use warnings; use Data::Dump; use List::Util 'max'; # 1. Configuration use constant UNIT => '* '; my @required_keys = qw(5 a foo); # 2. Read in and count the data my %counts = map { $_ => 0 } @required_keys; while () { ++$counts{$_} for split; } dd \%counts; # Verify hash contents # 3. Generate the histogram print "\nHistogram:\n"; my $max_len = max map { length } keys %counts; for (sort keys %counts) { printf "%*s: ", $max_len, $_; print UNIT for 1 .. $counts{$_}; print "\n"; } __DATA__ a -2 3 b 0xffff c 2 b a 4 a a 200 0xffff 17 a a c 3 200 201 b -2 b a b c a a 2 c -2