Search code examples
perllevenshtein-distance

Difference between 2 strings


I want to compare some strings like this

Previous -> Present

Something like

path 1 : 100 -> 112 --> 333 --> 500
path 2 : 100 -> 333 --> 500
path 3 : 100 -> 333 --> 500 --> 500
path 4 : 100 -> 112 --> 500

I need to compare path 1 with path 2, get the number that was in path 1 which doesn't exist in path 2 and store it in a database

Then compare path 2 with path 3 and do same thing. If it already exists then increment it. Otherwise insert the new number.

I know how to insert into a database and increment if the entry exists. What I don't know is how to loop through all those paths getting those values then deciding whether to insert into the database.

I have done some research, and I have heard of Levenshtein Edit Distance but I can't figure out how I should do it.


Solution

  • Your question appears to be:

    Given two lists of numbers, how can I tell which ones in list A aren't in list B?

    Hashes are useful for doing set arithmetic.

    my @a = ( 100, 112, 333, 500 );
    my @b = ( 100, 333, 500 );
    
    my %b = map { $_ => 1 } @b;
    my @missing = grep { !$b{$_} } @a;