ITmajor has asked for the wisdom of the Perl Monks concerning the following question:

Hello all. I want to create histogram (using gd graph) from data I have in an array. The problem is, I don't know how to count the number of times a number occurs. My array contains two sets of numbers I want to count: outward travel time and inward travel time.

Replies are listed 'Best First'.
Re: How to create histogram
by toolic (Bishop) on Oct 21, 2008 at 17:41 UTC
      Thanks
Re: How to create histogram
by afresh1 (Hermit) on Oct 21, 2008 at 17:48 UTC

    First, some examples of what you have, what you want, and what you have tried would be very helpful.

    One of my favorite ways to count items is with a hash, as in this example:

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; # Assuming these are [ in, out ] my @travel_times = ( [ 1, 5 ], [ 2, 5 ], [ 20, 15 ], ); my %count; foreach my $time (@travel_times) { $count{in }{ $time->[0] }++; $count{out}{ $time->[1] }++; } print Dumper \%count;
    Output is:
    $VAR1 = { 'out' => { '15' => 1, '5' => 2 }, 'in' => { '1' => 1, '2' => 1, '20' => 1 } };

    --
    andrew
Re: How to create histogram
by Corion (Patriarch) on Oct 21, 2008 at 17:35 UTC

    How would you do it using pen and paper?

Re: How to create histogram
by MidLifeXis (Monsignor) on Oct 21, 2008 at 17:51 UTC

    Is this homework?

    --MidLifeXis