Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Perl code for a Histogram

by Sandy (Curate)
on Jan 05, 2010 at 21:33 UTC ( [id://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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-04-26 01:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found