Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

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

UPDATE

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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2015-07-03 00:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (47 votes), past polls