So I've been doing some practice problems for both Perl & Python (kinda choosing between the 2) and I got a problem where I need to make my own diff algorithm just like Github's. I'm up to the point where I know that the Longest Common Subsequence problem is a big part of the solution. I used the wikipedia page for LCS as reference but I'm still having trouble figuring out the diff part.
I also realize there are already modules on CPAN like Algorithm:Diff, but this is mostly just for practice and those feel like cheating.
I figured out the python/pseudocode version of the algorithm but I plan on doing it with multi-dimensional arrays, which Perl doesn't seem to have.
Now I'm up to where I can successfully get the Longest Common Subsequence length in Perl.
Basically the pseudocode (almost python-like, but is supposed to be for Perl) I can think of is something like this:
function lengthOfLCS(string1, string2){
if length(string1) == 0 or length(string2) == 0:
return 0
else if string1[0] eq string2[0]:
return 1+ lengthOfLCS(stringA[1:], stringB[1:])
return max(lengthOfLCS(string1, string2[1:], lengthOfLCS(string1[1:], string2))
I haven't implemented it yet, but I think that's basically how I can calculate the length of LCS of two strings?
Output wise, it should return 4 against "HUMAN" & "CHIMPANZEE" (LCS = HMAN)
So what I'm asking is how do I get to printing Diffs using Perl from this point on? I'm aware that instead of only the length of the LCS, I should've a List/Array returned instead, which is doable by returning a multi-dimensional list in the LCS function and then processing it later on in a separate diff function.
I'm kinda new to Perl, so any pointers/tips would be greatly appreciated. Thanks.