J
James
I am trying to write to a database a hash of array (as seen by
__DATA__ in the code).
But somehow the first element of the array is missing. Any idea why?
The second time, it is working correctly.
$ cat run.pl
use DB_File;
use vars qw($db $x %h $k $v $i $key $val);
($db) = @ARGV;
%h = ();
$x = tie %h, "DB_File", $db, O_RDWR|O_CREAT, 0640, $DB_HASH;
write_db();
read_db();
untie %h;
undef $x;
sub write_db {
print "=== write $db ===\n";
for (<DATA>)
{
($k, @v) = split;
print "$k -> @v\n";
$r = \@v;
for $i (0..$#v) {
$h{$k}->[$i] = $r->[$i];
}
}
}
sub read_db {
print "=== read $db ===\n";
for ( $status = $x->seq($key, $val, R_FIRST); $status == 0; $status =
$x->seq($key, $val, R_NEXT) )
{
print "$key -> @{$val}\n";
}
}
__DATA__
aa 1 2 3
cc 678 99
zz foo fee fuu fun
$ ./run.pl mydb
=== write mydb ===
aa -> 1 2 3
cc -> 678 99
zz -> foo fee fuu fun
=== read mydb ===
aa -> 2 3
cc -> 99
zz -> fee fuu fun
TIA
JL
__DATA__ in the code).
But somehow the first element of the array is missing. Any idea why?
The second time, it is working correctly.
$ cat run.pl
use DB_File;
use vars qw($db $x %h $k $v $i $key $val);
($db) = @ARGV;
%h = ();
$x = tie %h, "DB_File", $db, O_RDWR|O_CREAT, 0640, $DB_HASH;
write_db();
read_db();
untie %h;
undef $x;
sub write_db {
print "=== write $db ===\n";
for (<DATA>)
{
($k, @v) = split;
print "$k -> @v\n";
$r = \@v;
for $i (0..$#v) {
$h{$k}->[$i] = $r->[$i];
}
}
}
sub read_db {
print "=== read $db ===\n";
for ( $status = $x->seq($key, $val, R_FIRST); $status == 0; $status =
$x->seq($key, $val, R_NEXT) )
{
print "$key -> @{$val}\n";
}
}
__DATA__
aa 1 2 3
cc 678 99
zz foo fee fuu fun
$ ./run.pl mydb
=== write mydb ===
aa -> 1 2 3
cc -> 678 99
zz -> foo fee fuu fun
=== read mydb ===
aa -> 2 3
cc -> 99
zz -> fee fuu fun
TIA
JL