Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions
 Discussion Forums
 HTML, XML, JavaScript...
 Software Reviews
 Editors,Others...
 Top100
 JavaScript Tutorials, ...
 Tutorials
 ASP, CSS, Databases...
 Discussion List
 FAQ, Roundup, Configure ...
 Authoring
 HTML, JavaScript, CSS...
 Design
 Layout, Navigation,...
 Graphics
 Tools, Colors, Images...
 Software
 Browsers, Editors, XML...
 Internet
 Domains, E-Commerce, ...
 WDVL Resources
  Intermdiate, Tutorials,...
 WDVL
 Discussion Lists, Top 100,...
 Technology Jobs


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Top 10 Articles
  1. Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions
  2. JavaScript Tutorial for Programmers
  3. Design
  4. JavaScript Tutorial for Programmers - Objects
  5. JavaScript Tutorial for Programmers - JavaScript Grammar
  6. JavaScript Tutorial for Programmers - Versions of JavaScript
  7. Cascading Style Sheets
  8. JavaScript Tutorial for Programmers - Embedding JavaScript
  9. JavaScript Tutorial for Programmers - Functions
  10. Authoring JavaScript
Domain Name Lookup
Search to find the availability of a domain name. Just enter the complete domain name with extension (.com, .net, .edu)

Getting Deep with Hashes

December 18, 2000

All of the hashes we've seen today have been one-level. Put another way, each key is simply associated with a scalar value. This works great with all the DBM's we've seen for storing these hashes onto disk-based files.

But the game changes when you want to store a multi-level hash. After all, Perl doesn't restrict key values to scalars -- the value of a key could be a list of scalars, or another hash, or a list of hashes, or a hash of lists, and so on ad infinitum. In fact, Perl hashes can be arbitrarily complex with many levels, and this is what makes Perl hashes especially powerful (and sometimes mind-numbingly confusing). None of the DBM approaches we've seen thus far can properly store such a multi-level hash, though.

Fortunately, there is a solution to everything. Well, not everything, but at least to this problem.

MLDBM is actually a module which sits on top of one of the other DBM's we've seen today, and lets you store multi-level hashes transparently, as if they were single-level hashes. The key to this, not to steal any of MLDBM's thunder, is rather simple and it's called serialization.

A serializer is an algorithm which essentially "flattens" a multi-level data structure into a single scalar value. Of course, like a compressed text file, you can't actually use the data structure in its serialized state, but it is a way to make such complex data portable. There are several serializers available for Perl, especially Data::Dumper and Storable. MLDBM essentially lets you hook up one of these serializers with one of your preferred DBM's, transparently so that the serializing and de-serializing happens without your intervention.

By default, MLDBM uses the SDBM database with the Data::Dumper serializer. But I prefer Storable, because it is faster. I also prefer DB_File for a DBM, and since we've seen that in use, let's illustrate using MLDBM with DB_File and Storable to tie a multi-level hash to disk. That's quite a mouthful.

use MLDBM qw(DB_File Storable);
use Fcntl;

my %car=(); tie (%car, "MLDBM", "car_data", O_CREAT|O_RDWR, 0666, $DB_File::DB_BTREE) ||
die "Could not open or create database."; $car{'JN1HU11P1HX875232'} =
{ 'make' => 'Nissan', 'model' => 'Maxima', 'year' => '1997', 'color' => 'evergreen' };
$car{'1GNDM15Z2HB187252 '} =
{ 'make' => 'Chevrolet', 'model' => 'Astro', 'year' => '1999', 'color' => 'black' };
print "Inventory contains: \n"; foreach (keys %car) {
print $_ . ":\t".$car{$_}{make}." ".$car{$_}{model}."\n";
} untie (%car);

When we use MLDBM we express our preference for the DB_File DBM and the Storable serializer. From there, we tie the hash and work with it rather normally. In this case, we again create a BTree hash. Our %car hash is a two-level hash, because the values of the top-level keys are themselves hashes. Each top-level key is a vehicle identification number, or VIN, representing a fictional inventory. The value for each VIN is a hash describing the car. A short output loop near the end of this script illustrates how we can dig into the hash levels.

There's nothing special about the code for managing this hash, and that's the point. Thanks to MLDBM, we deal with a multi-level hash just like any other, and the fact that it is being stored to disk, serialized, and de-serialized is entirely transparent.

Climbing the BTree
The Perl You Need to Know
Conclusion


Up to => Home / Authoring / Languages / Perl / PerlfortheWeb




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers