#!/usr/bin/perl
use strict;
use warnings;
# ----------------------------------------------
# make dummy data
# ----------------------------------------------
my @data;
foreach (1..500) {
push @data,int(rand(50)+1);
}
# ----------------------------------------------
# analyze data
# ----------------------------------------------
# what are the limits of my data, and define hash
# (all in one!)
my $min;
my $max;
my %hist;
my $most=0;
foreach my $datum (@data) {
$min = $datum if ! defined $min || $min > $datum;
$max = $datum if ! defined $max || $max < $datum;
$hist{$datum}++;
$most = $hist{$datum} if $most < $hist{$datum};
}
# ----------------------------------------------
# print the histogram
# ----------------------------------------------
print "\n\n";
no warnings 'uninitialized'; # UPDATED
foreach my $m (0 .. $most-1) {
printf "%05d ",$most-$m;
foreach my $i ($min .. $max) { # UPDATED
if ($hist{$i} >= ($most-$m)) {
print "x";
}
else {
print " ";
}
}
print "\n";
}
print " $min"," "x($max-$min),"$max\n";
I was bored
UPDATE
Note that there was no attempt to scale the output. I leave that up to you.