Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Perl code for a Histogram

by krish28 (Acolyte)
on Jan 05, 2010 at 21:08 UTC ( #815825=perlquestion: print w/replies, xml ) Need Help??
krish28 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have a long list of numeric values (which are quite random, as in, no constant interval b/w values, no ordering of the values, repetitive values) in an input file. I want to create a histogram which shows the number of instances that each value occurs in the whole'd be good if i can see the values in the x-axis vs the occurences of each value in the y-axis. Any idea on how to go about this a bit efficiently?? Thanks

Replies are listed 'Best First'.
Re: Perl code for a Histogram
by kennethk (Abbot) on Jan 05, 2010 at 21:14 UTC
Re: Perl code for a Histogram
by ww (Archbishop) on Jan 05, 2010 at 21:17 UTC
    We're a lot more inclined to help with efficiency questions when the poster includes the code already written to tackle a problem. See On asking for help and, perhaps, How do I post a question effectively?.

    It's a lot more timely to worry about efficiency after you have some working code.

      I've seen a couple of these in the past week or so, where the poster has no code to start off with, as if no attempt has been made to answer the question for themselves yet. It sort of feels like we're being asked to complete homework assignments!
        Sorry guys... I am new to the forum, will post my code alongwith any questions i have in the future...To clarify, I just wanted an idea to start off my code with, not any ready-made code to copy-paste into any hypothetical homework..
Re: Perl code for a Histogram
by Sandy (Curate) on Jan 05, 2010 at 21:33 UTC
    #!/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.

Re: Perl code for a Histogram
by Fletch (Chancellor) on Jan 06, 2010 at 15:24 UTC
    Simple perl -e 'system( qq{histosample.clj}, @ARGV )', provided [*]
    #!/usr/bin/env clj (ns histosample (:use [incanter core charts] [ :only (read-lines)])) (def data (if *command-line-args* (map #(Integer/parseInt %) (read-lines (first *command-line-ar +gs*))) (take 500 (iterate (fn [_] (int (rand 100))) 0)))) (let [max (reduce max data) min (reduce min data) nbins (Math/abs (- ma +x min))] (view (histogram data :nbins nbins :x-label "Value")))

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://815825]
Approved by toolic
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2017-12-16 00:51 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (447 votes). Check out past polls.