Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

DB_file and DB_HASH

by krisraman (Novice)
on Jun 05, 2002 at 00:06 UTC ( [id://171679]=perlquestion: print w/replies, xml ) Need Help??

krisraman has asked for the wisdom of the Perl Monks concerning the following question:

Monks I am using DB_File with Hash to store data.I am storing data in the format of Key|Value.My question is what should I do, if I want to read the data in the same order as Inserted.To make it clear, if I inserted 2|v1 and 3|v2, what should I do to read 2|v1 first follwed by 3|v2. Note: I tried using seq($key,$val,R_FIRST) and then using R_NEXT but no luck. Please help

Replies are listed 'Best First'.
Re: DB_file and DB_HASH
by crazyinsomniac (Prior) on Jun 05, 2002 at 00:08 UTC
      BTREE is faster too, according to a benchmark posted here a while back.
        Thanks but my question is not to get the data in the sorted order but the order in which the data was inserted.For exam if I did 323|Val and 121|Val, I would like to get 323 first.Sorry if I confused.Is there any way to achieve this using BTREE,please advise
Re: DB_file and DB_HASH
by stajich (Chaplain) on Jun 05, 2002 at 16:13 UTC
    This is a little tricky - if you weren't storing the data with DB_File I'd point you to Tie::IxHash which allows in-order storage/retrieval for Hashes. But of course that is already tieing the hash. The best way I can see to do this is to embed some information in your key that can be used to still sort the data and store it in a BTREE. Otherwise just keep two lists - one for your data (the DB_HASH you already have) and an array which stores the order you have inserted things. Of course it has to be kept up to date when you remove things and that can get obnoxious.

    Here is some code that would let you store things in-order. This hides the key revisions using filter_store_key and filter_fetch_key. The key thing here is that you can't use the hash functions but need to use OO interface instead {seq,put,get,del}. Hopefully you can get by with that. BerkeleyDB may have a nicer interface for what you need I'm not sure.

    #!/usr/bin/perl -w use strict; use DB_File; my %myhash; my $keycount = 0; # if you're going to go away and come back # from this program - make keycount persistent data somehow # simple interface my $filename = undef; # leave it undef for in-memory BDB my $b = new DB_File::BTREEINFO; $b->{'compare'} = \&_compare; my $db = tie(%myhash, 'DB_File', $filename, O_CREAT|O_RDWR, 0666, $b); $db->filter_store_key(sub { $_ .= "-".$keycount++;} ); $db->filter_fetch_key(sub { $_ =~ s/\-(\d+)$//; $_; } ); my ($key); $key = 'first'; $db->put($key,10); $key = 'second'; $db->put($key,12); $key = 'third'; $db->put($key,8.0); $key = 'fourth'; $db->put($key,'#five'); $key = undef; my ($val,$status); for( $status = $db->seq($key, $val, R_FIRST); $status == 0 ; $status = $db->seq($key,$val,R_NEXT) ) { print "$key -> $val\n"; } sub _compare { my ($key,$key2) = @_; my $rc; # a little bit of protection here in case # something wacky happens you don't get hit # with undef errors out the wazzo. if( (my ($orderpart1) = ($key =~ /\-(\d+)/)) && (my ($orderpart2) = ($key2 =~ /\-(\d+)/) )) { $rc = $orderpart1 <=> $orderpart2; } else { $rc = $key cmp $key2; } $rc; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://171679]
Approved by jarich
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2024-04-19 12:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found