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

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?

Replies are listed 'Best First'.
Re^5: Working out frequency statistics with perl
by jethro (Monsignor) on Jul 16, 2008 at 14:26 UTC
    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 );