I asked for help earlier in the CB and was pointed to the "References quick reference" page which i think helped me some, but I'm still pretty stuck. I have a mysql database, for the purposes of this question, we'll say it only has these two columns:
+--------------+---------+ | stamp | state | +--------------+---------+ | 200910021546 | Dormant | | 200910021546 | In Use | | 200910021352 | In Use | | 200910021352 | Dormant | | 200910021352 | In Use | | 200910021125 | In Use | | 200910021125 | In Use | | 200910021125 | Dormant |
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:
200910021546 = (Dormant,In Use) 200910021352 = (In Use, Dormant, In Use) 200910021125 = (In Use, In Use, 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 this
#!/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();
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.

In reply to Creating a hash of arrays from DB results by blaze

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.