monkfan has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

This is my first attempt with Plucene::Simple module.
I am trying to index files which are stored in "~/MyPerl/QA/data/"
and my code below (stored in ~/MyPerl/QA/).

I wonder what's wrong with my code below,
such that the print output gives:$VAR1 = {};
#! /usr/bin/perl -w use strict; use Plucene::Simple; use Data::Dumper; my %documents; my $index = Plucene::Simple->open( "~/MyPerl/QA/data/" ); for my $id (keys %documents) { $index->add($id => $documents{$id}); } $index->optimize; print Dumper \%documents;
What should I change if the desired output to be:
$VAR1 = { "file1.txt" => "The content...", #etcetera };
Regards,
Edward

Replies are listed 'Best First'.
Re: Indexing Files with Plucene::Simple
by tall_man (Parson) on Nov 03, 2004 at 20:22 UTC
    You create an empty hash called %documents, you loop over all keys in that hash (none, that is), and then you dump out the contents of that hash (again none). The Plucene::Simple module is not doing anything to fill that hash.

    The following would work, but I think you must be expecting something different to happen, such as an automatic index of all files in the directory you give. It won't do that.

    #! /usr/bin/perl -w use strict; use Plucene::Simple; use Data::Dumper; my %documents = (file1 => 'content1', file2 => 'content2'); my $index = Plucene::Simple->open( "~/MyPerl/QA/data/index" ); for my $id (keys %documents) { $index->add($id => $documents{$id}); } ...
      Thanks for the reply tall_man,

      Yes you are right, I am expecting an automoatic index to all files
      in ~/MyPerl/QA/data directory.

      Regards,
      Edward
      Update:
      Here are ways to populate hash from files with file name as hash key and its contents as values:
      . 1.Corion's
      map { $_ => do { local ($/,*F); open F, "<$_" or die "Error reading '$_': $!"; <F> }} glob('*') #or map{$_=>do{open my $fh,$_ or die"$_:$!";<$fh>}}glob('*')
      2.Aighearach's
      package MyPackage; sub make_hash { (local $/)=undef; my (@files,%hash); @files = @_; foreach my $file ( @files ) { open( my $fh, $file ) or next; $hash{$file} = <$file>; }; return \%hash; } __END__
      3.atcroft's
      foreach $filename (glob('*', '.*')) { open(DF, $filename) or die(); @{$hash{$filename}} = <DF>; close(DF); } #the snippet i mentioned would have had the contents in @{$hash{$fi +lename}}, #but you would have had to reassemble that back into a single strin +g, if that is what you needed)
        You might have better luck with SWISH-E. It's fast, easy to use, and has a good Perl interface.