?
Solved

Using GDBM with PERL

Posted on 2003-02-22
6
Medium Priority
?
1,414 Views
Last Modified: 2008-03-06
Hi, I want to use GDBM with Perl, I've tried searching the web over and over, and I get nothing.  How do I initialize a database? how do I store in it? how do i retrieve from it? Any help would be greatly appreciated.
0
Comment
Question by:ravaneli
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
6 Comments
 
LVL 2

Expert Comment

by:blinkie23
ID: 7999387
ok.  GDBM isn't standard to most perl installs.  and the web is apparently out to hide perl GDBM from users.  try this, it worked for me, even if slightly roundabout:

http://search.cpan.org/author/JHI/perl-5.8.0/MANIFEST

find "GDBM_File" on the page.  you should see six files.

ext/GDBM_File/gdbm.t
ext/GDBM_File/GDBM_File.pm
ext/GDBM_File/GDBM_File.xs
ext/GDBM_File/hints/sco.pl
ext/GDBM_File/Makefile.PL
ext/GDBM_File/typemap

download all of these into a new folder.  notice there is no README.  doesn't matter though, most perl module installations are the same process:

perl Makefile.PL   (creates "Makefile")

if you're not "root" or "admin", then you probably want to install it to a local working directory, like your homedir.  edit the Makefile, changing the PREFIX and following INSTALL*  variables to be a local directory like /home/me/lib/.  else, it will know where to put them by default

make test
make install


this will get things installed.  use the following line to do the most basic test.

perl -e 'use GDBM_File;'

if this runs smoothly without error, then you're doing well.
0
 
LVL 2

Expert Comment

by:blinkie23
ID: 7999404
how to use GDBM_File.  the perldoc documentation is quite short.  perl GDBM is supposed to mimic most of the traditional GNU gdbm behavior.  i've only see it accomplish this through tied variable use.  which is simple enough.

use GDBM_File;
tie %hash, 'GDBM_File', $filename, &GDBM_WRCREAT, 0640;

# using the hash array, anything you store in the hash will be put in the tied GDBM_File db
# thus it is persistant, even after the perl script terminates

$hash{"key"} = "value";
print $hash{"key2_that_i_stored_yesterday"},"\n";

untie %hash;

0
 

Author Comment

by:ravaneli
ID: 8003708
ok... I understand that a hash only has a value and a key for a every element.  But what if I want to store a third entry for the elements within the database.  How do I do that?

So if I have an essay for example, and want to store every word in it, how many times it occurs, and the line at which it occurs in the essay, how do i do that?
0
 
LVL 2

Accepted Solution

by:
blinkie23 earned 400 total points
ID: 8003880
normally in perl you can do multi-dimensional hashes or arrays.

$hash{key}{field1} = "this1";
$hash{key}{field2} = "this2";
$hash{key}{field3} = "this3";

if i remember correctly, the GBM libraries don't allow for multi-dimensional hash access via a tied variable to the db.  nor do they allow much in terms of references as values.  i remember this being an annoying problem.  first, test it out on your GDBM hash, as i've not worked with the gdbm variation, only with DB_File.


but, you are always able to do other creative data storage methods if you only have persistant access to a key/value pair for storage.

For instance, use "::" to delimit fields in your value.

$hash{key} = "this1::this2::this3";

a simple join or split is all that is needed to convert to this.  a small loop will fill or extract from a multi-dimensional hash/array.


similarly, you can try using the Freeze/Thaw methods of Perl, which supposively can turn (freeze) any type of datastructure into an ascii string representation (garble to you and i).  you store that, and simply thaw it out when you extract the value.  this may take some exploring though, check the web for documentations.  i believe it is:

use Storable qw(freeze thaw);

good luck.
0
 
LVL 20

Expert Comment

by:jmcg
ID: 9692029
Nothing has happened on this question in over 8 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
accept answer by blinkie23.

Please post any comments here within the next seven days. Moderators check comments here before acting on the recommendation. Experts: silence will likely be taken as assent.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question