Hello kris1511,

I tried several times your sample of code and the link that you provided us until I understand what you where trying to achieve. This is not what it is suppose to happen, this is a free forum that people are trying to assist each other for free. You need to work with your code make sure you provided us all the information that we need to replicate your problem so we can assist you as fast as possible and also to see that you have tried everything possible withing your knowledge before asking a question here. Of course this applies to all of us, not just you.

Having said that, the solution to your problem is provided bellow:

#!/usr/bin/perl use strict; use warnings; use feature 'say'; use List::Util qw( min ); sub editDistance{ my ($str1, $str2, $m, $n) = @_; return $n if $m == 0; return $m if $n == 0; return editDistance($str1, $str2, $m-1, $n-1) if substr($str1, $m, 1) eq substr($str2, $n, 1); return 1 + min ( editDistance($str1, $str2, $m, $n-1), #insert editDistance($str1, $str2, $m-1, $n), #remove editDistance($str1, $str2, $m-1, $n-1) ); #replace } my $str1 = "sunday"; my $str2 = "saturday"; say editDistance($str1, $str2, length($str1), length($str2)); __END__ $ perl test.pl 3

The reason that you where not getting the expected result is because you need to understand what happens with substr. Read again and again and see my update.

Update: From the nice implementation of fellow monk roboticus, just for testing purposes:

#!/usr/bin/perl use strict; use warnings; use feature 'say'; use List::Util qw( min ); sub editDistance{ my ($str1, $str2, $m, $n) = @_; return $n if $m == 0; return $m if $n == 0; return editDistance($str1, $str2, $m-1, $n-1) if substr($str1, $m, 1) eq substr($str2, $n, 1); return 1 + min ( editDistance($str1, $str2, $m, $n-1), #insert editDistance($str1, $str2, $m-1, $n), #remove editDistance($str1, $str2, $m-1, $n-1) ); #replace } my @pairs = ( [ "Sundayyy", "Saturday" ], [ "sunday", "saturday" ], ); for my $ar (@pairs) { my ($str1, $str2) = @$ar; say editDistance($str1, $str2, length($str1), length($str2)) . " < +$str1:$str2>"; } __END__ $ perl test.pl 5 <Sundayyy:Saturday> 3 <sunday:saturday>

Hope this helps, BR.

Seeking for Perl wisdom...on the process of learning...not there...yet!

In reply to Re: Edit Distance Implementation in Perl by thanos1983
in thread Edit Distance Implementation in Perl by kris1511

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.