Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: Perl code for a Histogram

by Sandy (Curate)
on Jan 05, 2010 at 21:33 UTC ( #815833=note: print w/ replies, xml ) Need Help??

in reply to Perl code for a Histogram

#!/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


Note that there was no attempt to scale the output. I leave that up to you.

Comment on Re: Perl code for a Histogram
Download Code

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://815833]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2016-02-08 09:12 GMT
Find Nodes?
    Voting Booth?

    How many photographs, souvenirs, artworks, trophies or other decorative objects are displayed in your home?

    Results (271 votes), past polls