Hello All, I am a newbie. I've been a round PERL for a while but never had to really get down to the nitty gritty of it. I am not a programmer and usually troll around looking for answers. Today I created a random number generator in hopes to pipe the output into a histogram set for plotting in GNUPLOT. While I understand how to generate a hash to tally the points, I am totally lost on creating bins, whether automatically spaced or with an argument from the user. Here is the code that I have so far:

Random number generator:

#!/usr/bin/perl -w use strict ; my $samplesize = $ARGV[0] ; my $max = $ARGV[1] ; chomp(my $prog = `basename $0`) ; if ($#ARGV < 1) { print "\nUsage: ./randomnumber.pl <samplesize> <max num>\n" ; exit ; } for (my $i = 0; $i<$samplesize; $i++) { my $range = $max ; my $random_number = int(rand($range)) - $max/2; print "$random_number \n"; }
Histogram creator:
#!/usr/bin/perl -w use strict ; my %bin ; my %size; my %value ; my $max = 0 ; my $min = 0 ; my $count ; while ( <STDIN> ) { my @line= split ; my $element = $line[0] ; $value{$element}++ ; } foreach my $i (sort {$a<=>$b} keys %value) { if ($i > $max) { $max = $i ; } if ($i < $min) { $min = $i ; } }
So if there are values -1, -1.5, -2, 2, 1, 0, 1, I would hope to see the following (if the bin range was between -2:2)

(-2) 3

(0) 1

(2) 3

Or something like that... Thanks in advance.


In reply to Help with Histograms by cmr72

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.