Hi Shawn,
On Wednesday 03 November 2010 16:25:09 shawn wilson wrote:
> hummm, i'm still having issues with these references. i'm hoping someone
> might see some flaw in my syntax or logic that might be easily corrected.
> so, here is what i have in all it's glory. what i want for output is return
> a csv with:
> search term,string from db,lines from input search string came from
>
> one search string will most definitely have multiple database matches and
> one search string (word) might be on multiple lines (it's why i put that
> last - so that i could easily keep columns straight).
>
> now, (thanks Shlomi) i'm just getting these errors which don't make sense
> to me:
> Global symbol "%word" requires explicit package name at
> ./namevsfield.plline 83. Global symbol "%word" requires explicit package
> name at ./namevsfield.plline 84.
This anachronistic message means you're using "use strict;" and have not
declared a variable or you are using a variable that does not exist. You can
add "use diagnostics;" at the top to turn on more verbose error descriptions,
that are useful for beginners (after you're experienced, they become a
nuisance).
In your case what happens is that you've written $word{$some_key} to access
the %word hash while you should have written $word->{$some_key} to access the
$word hash-*reference* (which is a scalar). See:
http://perl-begin.org/topics/references/
>
> i mean, word is a hash reference, but it should be defined inside of %data,
> no?
$word is, but not %word.
Now some comments on your code:
>
> after i get this working i'm definitely going to have to clean this up and
> start using $ref->{$key} notation. for now, this is what i have:
>
> #!/usr/bin/perl -Tw
>
You should use "use warnings;" instead of "-w".
> ##### WHAT #
> # What names return per search
> # search,db,line
> ###########
>
> use Carp::Assert;
> use strict;
> use DBI;
>
> # search left out: "owner.owner, owner.manown"
> my $searcher = "owner.manager, owner.owner, owner.manown";
>
> my $dbh = DBI->connect('DBI:mysql:db;host=localhost',
> 'user', 'pass')
> or die "Database connection: $!";
>
> open( FILE, "< $ARGV[0]" );
See:
http://perl-begin.org/tutorials/bad-elements/#open-function-style
Also don't reference $ARGV[0] directly:
http://perl-begin.org/tutorials/bad-elements/#subroutine-arguments
>
> my %data = {};
>
That's the wrong way to initialise an empty hash.
Just do: «my %data;». If you want to empty the hash later, you can do:
«%data = ();»
> while ( <FILE> ) {
>
> my $line = $_;
Don't do that:
http://perl-begin.org/tutorials/bad-elements/#assigning-from-dollar-underscore
>
> chomp ($line);
> my @word = split / /, $line;
>
>
> my $count = 0;
> while ( $word[ $count ] ) {
> $word[ $count ] =~ tr/^[\-a-zA-Z]//;
> $word[ $count ] =~ s/\'/\\\'/g;
>
> $data{ $word[ $count ] } = 0 unless defined( $data{ $word[
> $count ] } );
> ### from the last email, below doesn't look right, but i'm not sure how to
> fix it. it's not err'ing but...
> $data{ $word[ $count ] }{ $line } = $line;
>
> $count++;
> }
You should use a foreach loop here. testing for the truth of $word[$count] may
cause the loop to exit prematurely because "", 0, etc. are false in Perl.
Moreover, what are you trying to do in these lines:
> $data{ $word[ $count ] } = 0 unless defined( $data{ $word[
> $count ] } );
> ### from the last email, below doesn't look right, but i'm not sure how to
> fix it. it's not err'ing but...
> $data{ $word[ $count ] }{ $line } = $line;
> }
>
>
> for my $word ( keys %data ) {
>
> my ( $imo, $owner, $manown, $manager );
>
> my $select = qq/SELECT $searcher /;
> my $from = qq/FROM owner, spd /;
> my $where = qq/WHERE MATCH( $searcher )
> AGAINST('+$word' IN BOOLEAN MODE) /;
Use a placeholder here, and avoid SQL injections:
http://bobby-tables.com/
> my $join = qq/AND owner.num = spd.num/;
>
> my $query = $select . $from . $where . $join;
>
Why not use a here-document here?
> print "SQL: $query\n";
> my $sth = $dbh->prepare( $query );
> $sth->execute;
You are preparing similar statements time and again. Prepare it once and re-
use it with different parameters.
>
> while ( my $fields = $sth->fetchrow_arrayref ) {
> foreach my $field ( @{ $fields } ) {
> definition( $word, $field );
> }
> }
>
> }
>
> $dbh->disconnect;
>
>
> for my $word ( keys %data ) {
> while( my ($field, $type) = each %{ $data { $word } } ) {
You are using $data{$word} twice. You should abstract it into a variable using
each.
> print "$word,$field" if( $type eq 'field' );
> while( my ($line, $type) = each %{ $data { $word } } ) {
> print ",$line" if( $type eq 'line' );
> }
> }
> }
>
>
> sub definition {
>
> my ($word, $ufield) = @_;
> ### the below lines are what are err'ing when i run the script.
> if( defined( $ufield ) && !defined( $data{ $word { $ufield } } ) )
> { $data{ $word { $ufield } } = 'field';
Here's your error - you should do $word->{$ufield} instead of $word{$ufield}
Regards,
Shlomi Fish
P.S: please go over the rest of http://perl-begin.org/tutorials/bad-elements/
for enlightenment.
--
-----------------------------------------------------------------
Shlomi Fish http://www.shlomifish.org/
Best Introductory Programming Language - http://shlom.in/intro-lang
<rindolf> She's a hot chick. But she smokes.
<go|dfish> She can smoke as long as she's smokin'.
Please reply to list if it's a mailing list post - http://shlom.in/reply .
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/