in reply to using Algorithm::Diff for floats

It would be nice if Algorithm::Diff took a custom comparator function rather than a keygen function I think.

The easiest way I could see to solve this without having to dig around in the guts of A::L, is to bless each element of the arrays being compared and overload the 'eq' operator. You also have to overload the stringify operator to make this work.

#! perl -slw use warnings; use strict; use Data::Dumper; use Algorithm::Diff qw( diff ); { package myFloats; use overload 'eq' => \&cmp, '""' => \&stringy; sub new{ return bless \$_[ 1 ], $_[ 0 ] } sub cmp{ abs( ${ $_[ 0 ] } - ${ $_[ 1 ] } ) > 0.1 ; } sub stringy{ ${ $_[ 0 ] } } } my @a = map{ myFloats->new( $_ ) } qw( 1.0 2.1 3.2 4.0 4.9 5.8 ); my @b = map{ myFloats->new( $_ ) } qw( 1.0 2.0 3.0 4.0 5.0 6.0 ); my @d = diff( \@a, \@b ); print "@$_" for map{ @$_ } @d; __END__ P:\test>377814 - 1 2.1 - 2 3.2 + 1 2.0 + 2 3.0 - 4 4.9 + 4 5.0

Now whether that output makes any sense, I can't say. I've never really understood the output format of A::D. I added a little trace information into the cmp() sub

sub cmp{ print "cmp: @_"; abs( ${ $_[ 0 ] } - ${ $_[ 1 ] } ) > 0.1 ; }

and got this output:

P:\test>377814 cmp: 1.0 1.0 cmp: 5.8 6.0 cmp: 4.9 5.0 - 1 2.1 - 2 3.2 + 1 2.0 + 2 3.0 - 4 4.9 + 4 5.0

Which shows the overload is working, but it doesn't look like it is doing enough comparisons to me, but like I said, I never really understood the module. Anyway, maybe the technique will help you make sense of it.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
"Memory, processor, disk in that order on the hardware side. Algorithm, algoritm, algorithm on the code side." - tachyon

Replies are listed 'Best First'.
Re^2: using Algorithm::Diff for floats (DiffOld.pm)
by tye (Sage) on Jul 27, 2004 at 22:26 UTC
    It would be nice if Algorithm::Diff took a custom comparator function rather than a keygen function I think.

    Then use Algorithm::DiffOld instead (inlcuded with Algorithm::Diff). It is slower, of course.

    - tye        

      Thanks. I'd never even noticed that I had the old version on my machine. I'll have a play. Maybe it will highlight whether it's my code at fault or whether there is a bug in the new version.


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "Think for yourself!" - Abigail
      "Memory, processor, disk in that order on the hardware side. Algorithm, algoritm, algorithm on the code side." - tachyon
Re^2: using Algorithm::Diff for floats
by jim_neophyte (Sexton) on Jul 27, 2004 at 21:00 UTC

    well thank you very much. this will be a nice (i hope) intro to the overload module/pragma and Perl-OO.

    so far, i have some mods iaw your suggestion earlier to chg the module. i'll post it for comment if it seems to work.

    thanks again!