As you can see, the "stamp" column contains redundant data; what I'm trying to do is create a hash that has unique keys using the "stamp" column and a list for the value using the "state" column, the end result would be something like:+--------------+---------+ | stamp | state | +--------------+---------+ | 200910021546 | Dormant | | 200910021546 | In Use | | 200910021352 | In Use | | 200910021352 | Dormant | | 200910021352 | In Use | | 200910021125 | In Use | | 200910021125 | In Use | | 200910021125 | Dormant |
I'm using the code below (which isnt correct), but I just can't seem to get my head around the correct way to do this200910021546 = (Dormant,In Use) 200910021352 = (In Use, Dormant, In Use) 200910021125 = (In Use, In Use, Dormant)
Stamp is a date/time stamp, in the end all I'm trying to do is get a count for the number of "in use" channels for each unique time stamp, so once I've figured out how to get the data in an array, I'll loop through it and count the number of times "In Use" is found for each unique stamp. If anyone can help me, I'd greatly appreciate it.#!/usr/bin/perl use DBI; #use strict; my $user = "usern"; my $pass = "passw"; my $sql_s = "select stamp,state from pridata order by stamp limit 46"; my $dbh = DBI->connect("dbi:mysql:database=db1;",$user,$pass); my $sth = $dbh->prepare($sql_s); $sth->execute(); my $recs = $sth->fetchall_arrayref({}); foreach my $r(@{$recs}){ push(@$r->{stamp}, $r->state); } foreach my $l(@{$r->stamp}}){ print $l, "\n"; } $sth->finish(); $dbh->disconnect();
In reply to Creating a hash of arrays from DB results by blaze
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |