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. i mean, word is a hash reference, but it should be defined inside of %data, no? 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 ##### 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]" ); my %data = {}; while ( <FILE> ) { my $line = $_; 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++; } } 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) = @_; ### the below lines are what are err'ing when i run the script. if( defined( $ufield ) && !defined( $data{ $word { $ufield } } ) ) { $data{ $word { $ufield } } = 'field'; } } On Wed, Nov 3, 2010 at 9:03 AM, John W. Krahn <jwkr...@shaw.ca> wrote: > Shlomi Fish wrote: > >> >> On Wednesday 03 November 2010 09:27:14 shawn wilson wrote: >> >>> i'm getting errors when trying to print these hash refs out and i just >>> can't figure it out. here's the function: >>> >>> 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' ); >>> } >>> } >>> } >>> >>> >> 1. each %{$data}{$word} is incorrect. You probably want: >> >> each %{$data{$word}}. >> >> And you may wish to do: >> >> while (my ($word, $word_data) = each (%data)) { >> while (my ($field, $type) = each(%$word_data)) { >> >> 2. You are trying to iterate over %{$data{$word}} in two nested loops. >> This >> will confuse Perl to no end. >> > > No it will not. Perl will know exactly what to do. > > $ perl -le' > my %x = "a" .. "f"; > my $count = 5; > while ( my ( $k1, $v1 ) = each %x ) { > print "Loop 1: key=$k1 value=$v1"; > while ( my ( $k2, $v2 ) = each %x ) { > print "\tLoop 2: key=$k2 value=$v2"; > } > last unless --$count; > } > ' > Loop 1: key=e value=f > Loop 2: key=c value=d > Loop 2: key=a value=b > Loop 1: key=e value=f > Loop 2: key=c value=d > Loop 2: key=a value=b > Loop 1: key=e value=f > Loop 2: key=c value=d > Loop 2: key=a value=b > Loop 1: key=e value=f > Loop 2: key=c value=d > Loop 2: key=a value=b > Loop 1: key=e value=f > Loop 2: key=c value=d > Loop 2: key=a value=b > > > > John > -- > Any intelligent fool can make things bigger and > more complex... It takes a touch of genius - > and a lot of courage to move in the opposite > direction. -- Albert Einstein > > -- > To unsubscribe, e-mail: beginners-unsubscr...@perl.org > For additional commands, e-mail: beginners-h...@perl.org > http://learn.perl.org/ > > >