in reply to Creating a hash of arrays from DB results

Before your first foreach loop, declare a hash my %results. Then in the loop replace your push(@$r->{stamp}, $r->state) by:
push @{$results{$r->{stamp}}}, $r->state;

Or if you want to tally the various states right away:

($results{$r->{stamp}}->{$r->state})++;

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James