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

[MacPerl] dbm and associative arrays of lists

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")

Jean-François Jobidon
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com

===== Want to unsubscribe from this list?
===== Send mail with body "unsubscribe" to macperl-request@macperl.org