liamclimbguy9k has asked for the wisdom of the Perl Monks concerning the following question:
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:In my script, the code looks like 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;
The error happens on the line: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"; }
print $h{$intersection}{value}, "\n";
The MySQL db is structured like this:
Also, in my script '@titles' contains sentences that will be split by word into a hash.id|k |value --+---------+-------------- # |theword |1 (word count)
The first snippet of code returns
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.the 20
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Tie::DBI works in one script... not the other
by philipbailey (Curate) on May 18, 2015 at 23:08 UTC | |
by liamclimbguy9k (Initiate) on May 19, 2015 at 00:14 UTC | |
by graff (Chancellor) on May 19, 2015 at 01:15 UTC | |
by liamclimbguy9k (Initiate) on May 18, 2015 at 23:27 UTC |