Hello monks. I learned how to use Tie::DBI a long time ago, and found a snippet of code that fit perfectly into a script I was writing. This code throws a "Can't use an undefined value as a HASH reference..." when I use it in my new script, but works perfectly fine as it is. Seems odd to me, can't figure out why it works, but not when placed in my script.

So the snippet of code I used is this:
use Tie::DBI; use List::Compare; tie %h,'Tie::DBI',{db => 'mysql:db', table => 'table', key => 'k', user => 'root', password => 'rootpass', CLOBBER => 1}; %day = ('the' => '1', 'tuesday' => '2', 'wednesday' => '3'); $lc = List::Compare->new([keys %day],[keys %h]); @intersections = $lc->get_intersection; foreach $intersection (@intersections) { print $intersection, "\n"; print $h{$intersection}{value}, "\n"; } exit;
In my script, the code looks like this:
my %count; #tie hash to the MySQL DB tie my %h,'Tie::DBI',{db => 'mysql:db', table => 'table', key => 'k', user => 'root', password => 'rootpass', CLOBBER => 1}; #split @titles by word, then to-lower, then remv special chars my @intersections; my $lc; foreach my $title (@titles) { my @words = split(" ", $title); foreach my $word (@words) { $word =~ tr/A-Z/a-z/; $word =~ s/[^a-zA-Z0-9]*//g; $count{$word}++; print $word, "\n"; } } $lc = List::Compare->new([keys %count],[keys %h]); @intersections = $lc->get_intersection; foreach my $intersection (@intersections) { print $intersection, "\n"; print $h{$intersection}{value}, "\n"; }
The error happens on the line:
print $h{$intersection}{value}, "\n";

The MySQL db is structured like this:

id|k |value --+---------+-------------- # |theword |1 (word count)
Also, in my script '@titles' contains sentences that will be split by word into a hash.

The first snippet of code returns

the 20

Thanks in advance, I'm honestly expecting a really stupid bit of something I overlooked, but any advice would be welcome. Thank you. **EDIT** Updated based on philipbailey's comment. Moved the list comparison out of the for loop. Still getting undefined.

In reply to Tie::DBI works in one script... not the other by liamclimbguy9k

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.