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.
|
|---|
| 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 | |
by BrowserUk (Patriarch) on Jul 27, 2004 at 22:47 UTC | |
|
Re^2: using Algorithm::Diff for floats
by jim_neophyte (Sexton) on Jul 27, 2004 at 21:00 UTC |