Perl DB_File Query Key is not working but key exist

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 !

New contributor

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

New contributor

Yannick MOLINET is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật