Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Perl code for a Histogram

by Sandy (Deacon)
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 contemplating the Monastery: (4)
As of 2014-08-02 00:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (53 votes), past polls