ag4ve has asked for the wisdom of the Perl Monks concerning the following question:
ok, what i'm trying to do is, eventually, after everything is done, print the line that the results came from with the results.
first, i think there's an easier way to do this as i'm sorta tieing my brain in knots thinking about what array of what hash is going where. second, if this is the best way to do this, how do i print the string and not the array value (what it seems to be printing) in the final loop?
#!/usr/bin/perl use DBI; my $searcher = "owner.manager, owner.owner, owner.man"; my $dbh = DBI->connect('DBI:mysql:db;host=localhost', 'user', 'pass') or die "Database connection: $!"; open( FILE, "< $ARGV[0]" ); my %uword = (); my %seen = (); my $count = 0; my @data; my $key; my $i = 0; while ( <FILE> ) { my $line = $_; chomp ($line); my @word = split / /, $line; $count = 0; while ( $word[ $count ] ) { $word[ $count ] =~ tr/^[\-a-zA-Z]//; $word[ $count ] =~ s/\'/\\\'/g; $count++; } foreach my $string ( @word ) { if ( $uwordi{ $string }[ 0 ] == 1 ) { push @{ $uword{ $string } }, $line; next; } $uword{ $string }[ 0 ] = 1; push @{ $uword{ $string } }, $line; } } $count = 0; while ( $key = keys %uword ) { my ( $imo, $owner, $manown, $manager ); my $sstring = $uword{ $key }; my $select = qq/SELECT $searcher /; my $from = qq/FROM owner, data /; my $where = qq/WHERE MATCH( $searcher ) AGAINST('+ +$sstring' IN BOOLEAN MODE) /; my $join = qq/AND owner.num = data.num/; my $query = $select . $from . $where . $join; print "SQL: $query\n"; my $sth = $dbh->prepare( $query ); $sth->execute; $sth->bind_columns( \$manager, \$owner, \$man ); while ( $sth->fetch ) { if ( defined( $owner ) ) { $data[$count] = $owner; $count++; } if ( defined( $man ) ) { $data[$count] = $manown; $count++; } if ( defined( $manager ) ) { $data[$count] = $manager; $count++ } } foreach my $string ( @data ) { next if !defined ($string); if ( $seen{ $string }[ 0 ] == 1 ) { for my $value (1 .. $#{ $uword{ $string } } ) +{ push @{ $seen{ $string } }, $uword{ $s +tring }[ $value ]; } next; } $seen{ $string }[ 0 ] = 1; for my $value (1 .. $#{ $uword{ $string } } ) { push @{ $seen{ $string } }, $uword{ $string }[ + $value ]; } } } $dbh->disconnect;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: keeping context in a loop
by toolic (Bishop) on Oct 25, 2010 at 18:19 UTC | |
|
Re: keeping context in a loop
by jwkrahn (Abbot) on Oct 26, 2010 at 00:29 UTC | |
|
Re: keeping context in a loop
by ig (Vicar) on Oct 26, 2010 at 08:41 UTC | |
by afoken (Chancellor) on Oct 26, 2010 at 14:38 UTC |