Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Creating a hash of arrays from DB results

by blaze (Friar)
on Oct 05, 2009 at 20:12 UTC ( [id://799337]=perlquestion: print w/replies, xml ) Need Help??

blaze has asked for the wisdom of the Perl Monks concerning the following question:

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.

Replies are listed 'Best First'.
Re: Creating a hash of arrays from DB results (db)
by ikegami (Patriarch) on Oct 05, 2009 at 21:04 UTC

    Of course, the real solution is to let the DB do the work:

    SELECT stamp, COUNT(*) FROM Table WHERE state = "In Use" GROUP BY stamp

    I think that won't give stamps with zero "In Use". I think you'll need a subquery if you want that result.

      You guys rock, thanks a million!

      excellent catch ikegami!!
Re: Creating a hash of arrays from DB results
by CountZero (Bishop) on Oct 05, 2009 at 20:42 UTC
    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

Re: Creating a hash of arrays from DB results
by ramlight (Friar) on Oct 05, 2009 at 20:43 UTC
    This problem splits nicely into two parts: getting the data from the database and building the hash of arrays. I'm going to assume that you have (or will) solve the database portion. So here is how to build the hash of arrays:
    my %state_hash; while (<DATA>) { chomp; my ($stamp,$state) = split/\s*\|\s*/; print "$stamp $state\n"; push @{$state_hash{$stamp}}, $state; } print "\n"; for my $stamp (sort keys %state_hash ) { print "$stamp: @{ $state_hash{$stamp} }\n"; } __DATA__ 200910021546 | Dormant 200910021546 | In Use 200910021352 | In Use 200910021352 | Dormant 200910021352 | In Use 200910021125 | In Use 200910021125 | In Use 200910021125 | Dormant
    Which prints:
    perl stampstate.pl 200910021546 Dormant 200910021546 In Use 200910021352 In Use 200910021352 Dormant 200910021352 In Use 200910021125 In Use 200910021125 In Use 200910021125 Dormant 200910021125: In Use In Use Dormant 200910021352: In Use Dormant In Use 200910021546: Dormant In Use
Re: Creating a hash of arrays from DB results (no memory)
by ikegami (Patriarch) on Oct 05, 2009 at 21:00 UTC

    O(1) memory solution (provided the results are grouped by stamp):

    my $last; my $count = 0; while (my $row = $sth->fetch()) { my ($stamp, $status) = @_; if (defined($last) && $last ne $status) { print("$last: $count\n"); $count = 0; } $last = $stamp; ++$count if $status eq 'In Use'; } print("$last: $count\n") if defined($last);

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://799337]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-25 07:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found