Hash ref problem in Plucene::Simple (while searching a term)

E

Edward Wijaya

Hi,

I am just beginning to use Plucene::Simple module.
And the following code below tries to search a term in
indexed documents/files which are stored in a subdirectory "$tmp/"

Sample files I use are in this format:
Dir: /tmp/
file1.txt ---> content: test test
file2.txt ---> context: test test

However when I tried to print out the search result
it gives this error:

Can't use string ("test test") as a HASH ref while "strict refs" in
use at /usr/lib/perl5/site_perl/5.8.0/Plucene/Simple.pm line 235.

I have been scratching my head for a while to do this,
but still got no clue what went wrong.
Can anybody explain why this is?

Thanks and hope to hear from you again.

Regards,
Edward WIJAYA
SINGAPORE

__BEGIN__
#! /usr/bin/perl -w

use strict;
use Plucene::Simple;
use Data::Dumper;

my $dir = "tmp/*"; #sub directory where I store files to be indexed
my %documents = ();


%documents = map { $_ => do { local ($/,*F); open F, "<$_"
or die "Error reading '$_': $!"; <F> }} glob($dir);
# end of populating hash from files with file name as key and its content
as values, successfuly.
# $VAR1 = {
# 'tmp/file2.txt' => 'test test',
# 'tmp/file1.txt' => 'test test'
# };

#Begin Indexing
my $index = Plucene::Simple->open( $dir );

for my $id (keys %documents) {
$index->add($id => $documents{$id});
}
$index->optimize;

#do text search
my @ids = $index->search('test');
print Dumper \@ids;

__END__
 
T

Tad McClellan

Edward Wijaya said:
I am just beginning to use Plucene::Simple module.

it gives this error:

Can't use string ("test test") as a HASH ref while "strict refs" in
use at /usr/lib/perl5/site_perl/5.8.0/Plucene/Simple.pm line 235.


That means that perl is getting the string 'test test' when
it was expecting a reference to a hash instead.

# $VAR1 = {
# 'tmp/file2.txt' => 'test test',
# 'tmp/file1.txt' => 'test test'
# };

for my $id (keys %documents) {
$index->add($id => $documents{$id});


The docs for the module show that the add() method takes two
arguments: a string and a hash ref.

You are calling add with a string and a string instead.

I don't know Plucene, but maybe you need a data structure like

'tmp/file2.txt' => { text => 'test test'},

or perhaps you could just make the hash ref in the method call:

$index->add($id => { text => $documents{$id} });

In any case, you need to pass a hashref as add()'s 2nd argument.
 
E

Edward Wijaya

I don't know Plucene, but maybe you need a data structure like

'tmp/file2.txt' => { text => 'test test'},


Thanks a lot Tad,
Your advice has been very useful. I managed to solve the problem,
by creating datastructure as you suggested.

Using this subroutines to create the hash:

sub filehash_std {
my $dir = shift;

my @files = glob($dir);

(local $/) = undef;
my %hash =();

foreach my $file ( @files ) {
open( my $fh, $file ) or next;
$hash{$file}{text} = <$fh>;
}
return %hash;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top