CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
Problem: read a GDBM hash with 2 keys in perl

 



cscat
New User

Jun 29, 2010, 9:35 AM

Post #1 of 1 (1872 views)
Problem: read a GDBM hash with 2 keys in perl Can't Post

Hello all,

I have a perl script (look for create.pl down) that simply creates a db file. And then another read.pl that reads it. I have no problem in creating, but when I want to read the hash, the first key is retrieved fine; but the second key can't. please help.
db looks like this:
{TERM , DOCUMENT_NUMBER} -> VALUE

The problem is that TERM is shown but the next key which is DOCUMENT_NUMBER is not correct (always returns 0).

Thanks,
Reza


Code
-------------- create.pl -------------- 
#!/usr/bin/perl

use GDBM_File;
use Fcntl;

open( myfile, "term_doc_freq.out" ) or die;

my $lineNo;
my %wordInDoc = ();

tie (%wordInDoc, GDBM_File, "TermFreqDocid.db", O_CREAT|O_RDWR, 0644) || die ("Cannot create or open phone.db");

while( <myfile> )
{
my($line) = $_;
chomp $line;
$lineNo++;

@document = split (/ /, $line);
$docID = $lineNo;

foreach $term (@document) {
$wordInDoc{$term}{$docID}=0;
}
foreach $term (@document) {
++$wordInDoc{$term}{$docID};
}
}

untie (%wordInDoc);
exit;

-------------- read.pl --------------
#!/usr/bin/perl

use GDBM_File;
use Fcntl;

tie (%TermDocFreqHash, GDBM_File, "TermFreqDocid.db", O_RDONLY, 0777) ||
die ("Cannot open TermFreqDocid.db");

foreach $term ( sort( keys %TermDocFreqHash ) )
{
print $term." ";
my %tmp = $TermDocFreqHash{$term};

foreach $docID ( keys %tmp ) {
print $TermDocFreqHash{$term}{$docID};
}
}

untie (%TermDocFreqHash);
exit;

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives