Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: MySQL hashes with same key

by NetWallah (Abbot)
on Nov 16, 2012 at 05:01 UTC ( #1004122=note: print w/ replies, xml ) Need Help??


in reply to MySQL hashes with same key

Your code and comments are confusing. If you want to obtain comments for a known mac address, why dont you use the database to do the appropriate query:

"select comment from arp.filter where operation = 'filter' AND MAC='$mac_t'"
Also, Using fetchrow_arrayref instead of fetchrow_hashref will not make any difference to the resulting rows, as you seem to think it might.

             "By three methods we may learn wisdom: First, by reflection, which is noblest; Second, by imitation, which is easiest; and third by experience, which is the bitterest."           -Confucius


Comment on Re: MySQL hashes with same key
Download Code
Replies are listed 'Best First'.
Re^2: MySQL hashes with same key
by mje (Curate) on Nov 16, 2012 at 09:12 UTC

    He wasn't fetching using fetchrow_arrayref or fetchrow_hashref he was fetching with selectall_hashref and it does make a difference:

    SQL> create table mje(mac varchar(10), comment varchar(10)); SQLRowCount returns -1 SQL> insert into mje values('1226', 'comment1'); SQLRowCount returns 1 SQL> insert into mje values('1226', 'comment2'); SQLRowCount returns 1 SQL> insert into mje values('1226', 'comment3'); SQLRowCount returns 1 SQL> select * from mje; +-----------+-----------+ | mac | comment | +-----------+-----------+ | 1226 | comment1 | | 1226 | comment2 | | 1226 | comment3 | +-----------+-----------+ SQLRowCount returns -1 3 rows fetched SQL>
    perl -le 'use DBI; my $h = DBI->connect;nect; my $r = $h->selectall_ha +shref(q/select * from mje/, "mac"); use Data::Dumper; print Dumper($r +);' $VAR1 = { '1226' => { 'comment' => 'comment3', 'mac' => '1226' } };
    perl -le 'use DBI; my $h = DBI->connect;nect; my $r = $h->selectall_ar +rayref(q/select * from mje/); use Data::Dumper; print Dumper($r);' $VAR1 = [ [ '1226', 'comment1' ], [ '1226', 'comment2' ], [ '1226', 'comment3' ] ];

    If you use selectall_hashref or fetchall_hashref you need to provide a key column and if you have multiple rows where that key column has the same value you will lose some rows.

      Thanks - I had missed the subtlities of the second parameter to 'selectall_hashref'.

      However - my recommendation to use the database query to select comments for a mac stands, and answers the the questions he persists in posing.

                   "By three methods we may learn wisdom: First, by reflection, which is noblest; Second, by imitation, which is easiest; and third by experience, which is the bitterest."           -Confucius

        Hi, I was just trying to reduce repeated access to the database.
        I thought I could save it to a hash locally, and process the requested information from the hash.
        Thank you.
      So, don't do that. It's the wrong thing to do in this case. :-/
Re^2: MySQL hashes with same key
by jcrush (Acolyte) on Nov 16, 2012 at 15:34 UTC
    Thanks for the reply.
    I was using my smartphone, at night. I cleared up the wording, I hope.
    I would like to print out a list of comments for one address. If I search for mac aaaa.aaaa.aaaa.aaaa

    Printout

    Mac: aaaa.aaaa.aaaa.aaaa Comments: filtered for virus filtered by dmca filtered for excessive bandwidth filter on spoofed mac

    Thanks again.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1004122]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2015-08-02 21:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The oldest computer book still on my shelves (or on my digital media) is ...













    Results (13 votes), past polls