Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

looking for inspiration on 'compare'

by zioigor (Initiate)
on Mar 03, 2005 at 07:56 UTC ( [id://436114]=perlquestion: print w/replies, xml ) Need Help??

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

Bowing to higher level of knowledge, I am working with solid-rock DB_File module, but something cracked my faith: i tried to ovverride $DB_BTREE->{'compare'} routine with a String::Approx 'adist' one. Belief and RTFM suggested to create a 'callback' with an easy template, something like ... "hey man these are 2 parameters, do some kind of comparison and give me back -1, 0 or 1" as the 'cmp' example on documentation..." so, after checking out the only working example routine with case-insensitive comparison on manual, i tried mine:
# ------------------------- sub DBFile_Compare_Distance #-------------------------- { my ($k1, $k2) = @_ ; my $dist = adist ( uc $k1 , uc $k2 ); my $val = undef; if( $dist < -1 ) { $val = -1; } elsif( $dist > 1 ) { $val = 1; } else { $val = 0; } $val; }
- .db file gets created and i checked the binaries
- all data seem to fit
BUT
when i try to read out key/value pairs, i got "undef" on values for which comparison function returned -1.
Any clue on how to make a Compare function different from the one on manual?
many prayers are rising towards the Monastery
thanks igor

Replies are listed 'Best First'.
Re: looking for inspiration on 'compare'
by fizbin (Chaplain) on Mar 03, 2005 at 14:07 UTC
    when i try to read out key/value pairs, i got "undef" on values for which comparison function returned -1.
    This statement doesn't make sense. A comparison function is passed two keys. Yet when you read out key/value pairs, you get one key at a time. You can't look at a key and say "Ah, for this key, the comparison function returned -1". You can look at a pair of keys and say "For this pair of keys, in this order, the comparison function returned -1." But as it stands, your statement makes no sense.
    -- @/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/
Re: looking for inspiration on 'compare'
by PodMaster (Abbot) on Mar 03, 2005 at 08:19 UTC
    BUT
    when i try to read out key/value pairs, i got "undef" on values for which comparison function returned -1.
    That sounds fishy, are you sure you're not storing undef? A custom compare subroutine should not effect the values stored (and its most likely that its not).
    Any clue on how to make a Compare function different from the one on manual?
    You already have one, so just like that.

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

      Hi, yes i am logging every step before and after the ->put() infact I do agree with what you say. But every check i made goes in the direction u suggest. I do print with Dumper the tied hash, and those read return undef only for -1 comapre values. On binary file i checked and data are there. I mentioned the minus one just because is the only clue i have ... on non working values ...
        ...On binary file i checked and data are there. I mentioned the minus one just because is the only clue i have ... on non working values ...
        So which do you think is more likely, that there is something seriously broken with a very old and thoroughly tested module, or that you're doing something boneheaded? You need to submit a self contained example which demonstrates your bug.

        MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
        I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
        ** The third rule of perl club is a statement of fact: pod is sexy.

Re: looking for inspiration on 'compare'
by jZed (Prior) on Mar 03, 2005 at 14:42 UTC
    This is of no help in figuring out your current problem but may be of interest for the future. Were you aware that the DBI distribution now includes DBD::DBM? This module provides a DBI/SQL front-end to any DBM file including DB_File and BerkeleyDB either with or without MLDBM and using your choice of serializer. Naturally, SQL provides a number of comparison operators so, at a minimum, you could check your file (without converting it) with DBD::DBM, just to make sure it has in it what you expect.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-04-23 20:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found