I’m new with perl.
I’m writing a little code to lookup value in db from key.
If I do a loop on the %hash for keys and value : I can see my keys and my values.
But, when I want to load a specific key, the value is empty.
$database_obj = tie(%db_hash, 'DB_File', $database_name,
O_RDONLY, 0644, $DB_HASH) ||
fatal_exit "Cannot open database %s: $!", $database_name;
# This work - Console display [myKey1]: {myVal1} ...
while (my ($key, $val) = each(%db_hash))
{ print "[$key]: {$val}n"; }
# but, this doesn't work - $value is empty - I want to get myVal1
$value = $db_hash{myKey1};
db_dump show this header :
VERSION=3
format=bytevalue
type=hash
h_nelem=4098
db_pagesize=4096
HEADER=END
If anyone can help me ? Thanks in advance.
Regards,
Yannick
I have try to manipulate the key format with or without quote.
Additionnal test 1
#!/usr/bin/perl
use strict;
use warnings;
use DB_File;
use Fcntl;
use Sys::Syslog qw(:DEFAULT setlogsock);
my ($database_obj, %db_hash);
my $database_name="./mydb.db";
$database_obj = tie(%db_hash, 'DB_File', $database_name,
O_RDONLY, 0644, $DB_HASH) ||
print "Cannot open database %s: $!", $database_name;
print "DUMP ...n";
while (my ($key, $val) = each(%db_hash))
{ print "[$key]: {$val}n"; }
my $search="mykey.1";
print "ONLY $searchn";
my $value = $db_hash{$search};
print "Value: $valuen" unless $value;
Result:
DUMP ...
[mykey.1]: {myVal1;myVal-1}
[mykey.2]: {myval2}
ONLY mykey.1
Use of uninitialized value $value in concatenation (.) or string at test.pl line 25.
Value:
keys and values can contains . -> it seems a problem ?
Additionnal test 2
#!/usr/bin/perl
use strict;
use warnings;
use DB_File;
use Fcntl;
use Sys::Syslog qw(:DEFAULT setlogsock);
use Data::Dumper;
$Data::Dumper::Useqq=1;
my ($database_obj, %db_hash);
my $database_name="./mydb.db";
$database_obj = tie(%db_hash, 'DB_File', $database_name,
O_RDONLY, 0644, $DB_HASH) ||
print "Cannot open database %s: $!", $database_name;
#print "DUMP ...n";
# while (my ($key, $val) = each(%db_hash))
# { print "[$key]: {$val}n"; }
print Dumper %db_hash;
my $search="mykey.1";
print "ONLY $searchn";
my $value = $db_hash{"$search"};
print "Value: $valuen" unless $value;
result:
$VAR1 = {
"mykey.1" => "myVal1;myVal-1",
"mykey.2" => "myval2"
};
Sorry for delay, but it seems that we are not in the same timezone.
Data are populated with postmap
I see that my strings are terminated with (null terminator ?), so I try to add it in the lookup.
#!/usr/bin/perl
use strict;
use warnings;
use DB_File;
use Fcntl;
use Sys::Syslog qw(:DEFAULT setlogsock);
use Data::Dumper;
$Data::Dumper::Useqq=1;
my ($database_obj, %db_hash);
my $database_name="./mydb.db";
$database_obj = tie(%db_hash, 'DB_File', $database_name,
O_RDONLY, 0644, $DB_HASH) ||
print "Cannot open database %s: $!", $database_name;
#print "DUMP ...n";
# while (my ($key, $val) = each(%db_hash))
# { print "[$key]: {$val}n"; }
print Dumper %db_hash;
my $search="mykey.1";
print "ONLY $searchn";
my $value = $db_hash{"$search"};
print "Value: $valuen" unless $value;
result :
$VAR1 = {
"mykey.1" => "myVal1;myVal-1",
"mykey.2" => "myval2"
};
ONLY mykey.1
No value return so $value seems to be null.
Additionnal info
@clamp if I remove “unless $value” it’s working.
I need to add at the end of the key !
Yannick MOLINET is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
7
As mentionned in my initial question, I’m using postmap to generate the db files.
As mentionned here : https://metacpan.org/pod/DB_File#An-Example-the-NULL-termination-problem. C program add a NULL Termination at the end of the string (key and value).
Postmap is written in C so it add a NULL Terminaison.
So, my problem is due to my need to read a database generate by a C program with perl
To be able to match correctly the key, it’s need to add “” add the end of the key
$db_hash{"$search"}
and not
$db_hash{"$search"}
Yannick MOLINET is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.