in reply to Re^2: Working out frequency statistics with perl
in thread Working out frequency statistics with perl

I don't understand the subroutine that was posted earlier. I don't understand what I am supposed to pass to items?

A list of numbers from which you want to build your histogram. And it doesn't have to be sorted.

Your sample data generates this histogram for me:

0: 67 30: 73 60: 93 90: 75 120: 26 150: 18 180: 10 210: 5 240: 2 270: 1 300: 1 330: 2 360: 1 390: 1 450: 1 840: 1

Replies are listed 'Best First'.
Re^4: Working out frequency statistics with perl
by wishartz (Beadle) on Jul 16, 2008 at 12:48 UTC
    Please excuse my ignorance, but I'm not sure where to call the subroutine build histogram, and how to print out the returned result hash. I think I'm going about this in the wrong way. I tried this:
    open(READMAP, "$command |") || error_exit("Cannot run readmap, $!"); +#run the readmap command while(<READMAP>) { #loop through the output of the readmap command if (/StageTime/){ @fields = split/\s+/; + #Split the output by white space chop $fields[9]; + # Remove the period after the last digit $diskxStats[$i]{'filesize'}=$fields[2]; $diskxStats[$i]{'ftptime'}=$fields[5]; $diskxStats[$i]{'stagetime'}=$fields[9]; if ( $fields[5] != 0 ){ $diskxStats[$i]{'transferRate'} = $fields[2] / $fields[5]; } else{ $diskxStats[$i]{'transferRate'} = $fields[2]; } if ( $fields[9] != 0 ){ $diskxStats[$i]{'stagerate'} = $fields[2] / $fields[9]; } else{ $diskxStats[$i]{'stagerate'} = $fields[2]; } $i++; } } for $i (0 .. $#diskxStats ) { build_histogram ($bin_stage, $diskxStats[$i]{'stagetime'}); } sub build_histogram { my ($bucket_size, @items) = @_; my %result; for (@items) { my $bucket = $bucket_size * floor($_ / $bucket_size); # print "$bucket\n"; $result{$bucket}++; } return %result; }
    Which is obviously incorrect, but not sure what I need to change to get the results you got?
      In your case you would have to extract the single diskstats into an array again :
      build_histogram ($bin_stage, map $_->{'stagetime'},@diskxStats );
      This is equivalent to the following code which might or might not be easier to understand:
      my @statetime=(); foreach (@diskxStats) { push @statetime, $_->{'stagetime'}; } build_histogram ($bin_stage, @statetime );