http://www.perlmonks.org?node_id=1004111

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

# # # # # # # # # # # # SQL MAC Query # # # # # # # # # # # # our $smq = $dbh->prepare("select distinct mac,comment from arp.filter where operation = 'filter' order by mac") or die "Couldn't prepare statement: " . $dbh->errstr; # # # # # # # # # # # # # # # # # SQL Distinct MAC Table # # # # # # # # # # # # # # # # # $smq->execute() or die "Couldn't execute statement: " . $smq->errstr; print "# SQL Distinct MAC HASH #\n"; # +----------------+-------------------------------------+ # | mac | comment | # +----------------+-------------------------------------+ # | 1226.b0ab.317e | roving filtered mac | # | 1226.b0ab.317e | roving filtered mac [force filter | # | 1226.b0ab.317e | [thor2] Excessive Campus Bandwidth | # +----------------+-------------------------------------+ # 3 rows in set (0.04 sec) our $mac_t = "1226.b0ab.317e"; my $ref = $dbh->selectall_hashref("select distinct mac,comment from arp.filter where operation = 'filter' order by mac", 'mac'); foreach my $id (keys %$ref) { if ($mac_t eq $ref->{$id}{mac}) { print "Found a row: mac = $ref->{$id}{mac}, comment = $ref->{$id}{comment}\n"; } }
The results printed are missing all 3 rows of data
# SQL Distinct MAC HASH # Found a row: mac = 1226.b0ab.317e, comment = [thor2] Excessive . . . # END SQL Distinct MAC HASH #
Am I forced to use Arrays, and if so how is the best way to do that?
If using an Array
What happens if I want search one address, and print out all comments attached to that one mac address? (Reason for my hash usage)
For example, if I have 100 macs, and if one is in the database, then print out all the comments on that mac. Thank You, very much for the help Perl Monks !