Gossamer Forum
Home : General : Perl Programming :

Hashes and speed/efficiency (two questions)

Quote Reply
Hashes and speed/efficiency (two questions)
Two questions:

1) Does it matter for speed and efficiency if my add.cgi writes an record to my database file of 1500 lines or one of 20 lines? (I use the following code.) The because now my add.cgi directly writes to my main database of 1000 records and I'm wondering if I should let add.cgi write to a temp file and update the main file every night. Would that make the add routine that takes a long lime shorter?

Code:
# Update the database.

open (DB, ">>$db_file_name") or &err("Error. Reason: $!");
flock(DB, 2) unless (!$db_use_flock);
print DB &join_encode(%in);
close DB;



2) I always used to define multiple hashes like:

Code:
%contactname =
'Jan' => 'Full name of Jan',
'Peter' => 'Full name of Peter',
'Micheal' => 'Full name of Micheal',
'Bill' => 'Full name of Bill'
);

%contactphone = (
'Jan' => 'Phone number of Jan',
'Peter' => 'Phone number of Peter',
'Micheal' => 'Phone number of Micheal',
'Bill' => 'Phone number of Bill'
);

And read them out with:

Code:
my $lookup = 'Bill';
my $name = $contactname{$lookup} || 'unkown';

But after reading some posts the last days I'm wondering if something like the following is possible: (It would certainly make things easier in scripting. And my hash files shorter!)


Code:
%contactname =
'Jan' => 'Full name of Jan', 'Phone number of Jan',
'Peter' => 'Full name of Peter', 'Phone number of Peter',
'Micheal' => 'Full name of Micheal', 'Phone number of Michael',
'Bill' => 'Full name of Bill', 'Phone number of Bill'
);
Quote Reply
Re: [Perlboy Chris] Hashes and speed/efficiency (two questions) In reply to
Regarding your hash question, if you use two values for one key you need to enclose the whole lot with [ ] eg...

%hash = ( KEY => ['Val1','Val2'] );
Quote Reply
Re: [Perlboy Chris] Hashes and speed/efficiency (two questions) In reply to
Code:
%contactname = (
Jan => {
full_name => 'Full name of Jan',
phone => 'Phone number of Jan',
},
Peter => {
full_name => 'Full name of Peter',
phone => 'Phone number of Peter',
},
Micheal => {
full_name => 'Full name of Micheal',
phone => 'Phone number of Michael',
},
Bill => {
full_name => 'Full name of Bill',
phone => 'Phone number of Bill',
},
);

print $contactname{Jan}->{phone};
Quote Reply
Re: [Mark Badolato] Hashes and speed/efficiency (two questions) In reply to
Or just...

print $contactname{Jan}{phone};