Create cdb file using perl

To create cdb file in perl is quite easy when you have the hash ready.

See example below.


#!/usr/bin/perl
use strict;

use CDB_File;

my %t = (
            'a' =>'apple', 
            'b' => 'banana',
        );

CDB_File::create %t, "<cdb file name>", "<temporary file name>";


If you have the key-value pairs in a file, you can parse through it and insert into the cdb by getting the hash ready, or by the pairs.

Say your data file contain this.

a,apple
b,banana

Perl code for getting this flat file data into cdb :

#!/usr/bin/perl
use strict;

use CDB_File;
my $cdb = new CDB_File("<cdb file name>", "<temporary file name>");

open (DATA, "<data file name>");

while (<DATA>) {
    chomp();
    my @data = split /,/;
    $cdb->insert($data[0],$data[1]);
}

$cdb->finish;

Comments

  1. thanks a lot. I was looking for a simple example.

    ReplyDelete

Post a Comment

Popular Posts