這裡有個人做了跟我昨晚一模一樣的事
有人有提供解釋
http://bumppo.net/lists/macperl/1999/06/msg00504.html
(為避免以後找不到,原文轉貼)
- To: macperl@macperl.org
- Subject: [MacPerl] dbm and associative arrays of lists
- From: jean-Francois Jobidon <jfjobidon@yahoo.com>
- Date: Wed, 30 Jun 1999 14:32:17 -0700 (PDT)
Hi
First, thank for the help with my last question.
Here is another one :
I'd like to store informations in a database using a hash with a list
instead of a single value. And then I would like to change only one
item of a list.
Here is my code for creating the database :
$file = "my_dbm";
dbmopen (%alpha, $file, 0666) || die "can't create $file\n";
@list1 = qw(item10 item11 item12 item13);
@list2 = qw(item20 item21 item22 item23);
@list3 = qw(item30 item31 item32 item33);
$key1 = "k1";
$key2 = "k2";
$key3 = "k3";
%hash = (
$key1 => [@list1],
$key2 => [@list2],
$key3 => [@list3]
);
%alpha = %hash;
dbmclose (%alpha);
And now, the code for reading :
$file = "my_dbm";
dbmopen (%beta, $file, 0666) || die "can't open «$file»\n";
@mat = (keys(%beta));
$keyX = pop (@mat);
@list = @{$beta{$keyX}};
$card = $#list;
#$card is the number of items in a list
foreach $item (keys (%beta)) {
print "the key is $item", "\nthe values are :\n";
for ($k = 0; $k <= $card; $k++) {
print "the value of item $k is : $beta{$item}[$k]\n";
}
}
dbmclose (%beta);
The problem is $card is set to -1, wich means the list is empty.
What I'm doing wrong ?
Do I must join all the lists into a single value before writing the
database and then split it each time I want to modifie an item ? It
seems long to me, just to change a single value (like "element11"
instead of "item11")
Thanks
Jean-François Jobidon
jfjobidon@yahoo.com
On Wed, Jun 30, 1999 at 02:32:17PM -0700, jean-Francois Jobidon wrote: > The problem is $card is set to -1, wich means the list is empty. > What I'm doing wrong ? > Do I must join all the lists into a single value before writing the > database and then split it each time I want to modifie an item ? It > seems long to me, just to change a single value (like "element11" > instead of "item11") > If you want to store multi-level hashes in a database, you need to use something like the MLDBM module. Otherwise, your outer references are turned into strings, and the inner levels are lost. http://www.perl.com/CPAN//local/modules/by-module/MLDBM/ Ronald