[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl] MacPerl & Databases



Hello Folks,

I've been trying to write a MacPerl script which should simply read in a 
tab-delimited text file, and create a corresponding DBM file to hold the 
data. The script finally works, but during its development I ran into a 
few issues I'd like to ask you about:

Hash definition problem
=======================
Each line of the text file holds one record with 11 fields. My intent was 
to tie a hash to a database where each value of that hash was another 
hash (which I'll call a sub-hash) with keys being the field names, and 
values being the associated values. (So I'm duplicating the field names 
for each record in the database.)

The following code reads in the data from the text file. In Example 1, 
however, precisely one value in each sub-hash ends up empty. It happens 
to correspond to the sub-hash's first entry (which happened to be the 
third field in the text-file database). Example 2 manages to correct the 
problem by repeating the %database assignment if the sub-hash field is 
empty. 

Example 1:
----------
while (defined ($line = <SOURCE_FILE>)) {
     chomp($line);
	    @source_fields = split(/$source_delim/, $line);
	    if ($#field_names != $#source_fields) { die("Length mismatch.") }
	    @temp{@field_names} = @source_fields;
	    foreach $key (keys %temp) {
		        $database{$source_fields[0]}{$key} = $temp{$key};
	    }
}

          Problem: $database{$source_fields[0]}{'name_last'} is empty
                   for all $source_field[x]!

Example 2:
----------
while (defined ($line = <SOURCE_FILE>)) {
     chomp($line);
	    @source_fields = split(/$source_delim/, $line);
	    if ($#field_names != $#source_fields) { die("Length mismatch.") }
	    @temp{@field_names} = @source_fields;
	    foreach $key (keys %temp) {
		        $database{$source_fields[0]}{$key} = $temp{$key};
		        unless ($database{$source_fields[0]}{$key}) {
			            $database{$source_fields[0]}{$key} = $temp{$key};
		        }
	    }
}

          Problem Solved: $database{$source_fields[0]}{'name_last'}
                          now contains data for all $source_field[x]!

I have no idea why this happened, so I thought I'd ask the list.

Database Size
=============
My script uses AnyDBM_File, and the resulting DBM file given a 78k source 
text file turned out to be 28k. When I switched to DB_File, MacPerl 
crashed with 'out-of-memory' errors. I then performed a small (10 record) 
test using the two database implementations and the DB_File required 
*megabytes* while AnyDBM_File required only a few kilobytes.

Is this normal, or is it likely I've done something wrong?

Locking & Portablility
======================
While I'm presently testing my Perl scripts locally on a Mac (running 
Quid Pro Quo) I'd eventually like to host the script on my Unix server. 
Is there a portable way to implement database file locking to prevent 
contention problems?

Thanks very much in advance!

Kind regards


Matt Henderson   |   matt@exponet.net   |    www.exponet.net
                 .                      .    
E x p o n e t   M e d i a  T e c h n o l o g i e s   G m b H
Full-Service Web Development & Engineering Services Provider


***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch