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

In reply to Re: using Algorithm::Diff for floats by BrowserUk
in thread using Algorithm::Diff for floats by jim_neophyte

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.