ok, i think i understand most of the documentation you sent (well,
http://bobby-tables.com/ didn't work, but i read the rest). below is my
modified code (i'm hoping it is better than the code i posted before as that
might mean that i possibly learned something :) ).

i also have a couple questions:
1. what's the reason for 'use warnings' instead of 'perl -w'? just
readability or is there something more?
2. i read the docs you referenced @ARGV on but i didn't see any reference to
it. granted, i haven't googled this further, however what's the reason for
not directly referencing it? the same reason as not referencing $_? -
readability?
3. i'm aware of the sql injection possibilities with this code and though i
have no intention of using placeholders (it's a PITA) i will do verbose
checking on my strings as well as looking at the dbd taint option. however,
i don't understand what you mean here:
"You are preparing similar statements time and again. Prepare it once and
re-
use it with different parameters."
can you rephrase that or give me an example of what you mean?
4. Per this:
"Moreover, what are you trying to do in these lines:"
i'm trying to see if the word is defined in that hash, which was erroring,
so i removed it.
      $data->{ $aword } = 0 unless defined( $data->{ $aword } );
if it's not, define it. then make define the value to the below to 'line'.
      $data->{ $aword }->{ $line } = 'line';
... it was supposed to be the word line and not the variable for me to check
against later.


however, now it's in an infinite loop, printing out the first word of the
file twice, i'm guessing it's stuck on the inner loop and never gets to the
"\n".... and if i change $data->{ $aword }->{ $line } = 'ln' it just prints
'word,' over and over whereas the below will print 'wordword,' over and
over.... or, here:

SQL: SELECT owner.manager, owner.owner, owner.manown FROM owner, spd WHERE
MATCH( owner.manager, owner.owner, owner.manown ) AGAINST('+APL' IN BOOLEAN
MODE) AND owner.num = spd.num
SQL: SELECT owner.manager, owner.owner, owner.manown FROM owner, spd WHERE
MATCH( owner.manager, owner.owner, owner.manown ) AGAINST('+something' IN
BOOLEAN MODE) AND owner.num = spd.num
APL,,APLAPL,,APLAPL,,APLAPL,,APLAPL,,APLAPL,,APLAPL,,APLAPL,,APLAPL,,APLAPL,,APLAPL,,APLAPL,,APLAPL,,APLAPL,,APLAPL,,APLAPL,,APLAPL,,APLAPL,,APLAPL,,APLAPL,,APLAPL,,APLAPL,,APLAPL,,APL


#!/usr/bin/perl -T

##### WHAT #
# What names return per search
# search,db,line
############

use Carp::Assert;
use diagnostics;
use strict;
use warnings;
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]" );

my $data;

while ( my $line = <FILE> ) {

        chomp ($line);
        my @word = split / /, $line;


        my $count = 0;
        foreach my $aword ( @word ) {
                $aword =~ tr/^[\-a-zA-Z]//;
                $aword =~ s/\'/\\\'/g;

#               $data->{ $aword } = 0 unless defined( $data->{ $aword } );
                $data->{ $aword }->{ $line } = 'line';

                $count++;
        }
}


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) /;
        my $join =              qq/AND owner.num = spd.num/;

        my $query = $select . $from . $where . $join;

        print "SQL: $query\n";
        my $sth = $dbh->prepare( $query );
        $sth->execute;

        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 } } ) {
                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) = @_;

        if( defined( $ufield ) && !defined( $data->{ $word }->{ $ufield } )
) {
                        $data->{ $word }->{ $ufield } = 'field';
        }
}

On Wed, Nov 3, 2010 at 12:50 PM, Shlomi Fish <shlo...@iglu.org.il> wrote:

> 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 .
>

Reply via email to