Difference between 2 strings

Tag: perl , levenshtein-distance Author: alanwei863 Date: 2013-12-25

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.

Please post the final output you're expecting.
What if path 2 was 100 -> 112 --> 400 --> 500. Is that possible?
I have changed your question so that it is more readable. Please make sure it is correct
Please describe your data. Do you want to store all unique pairs, for example 100/112, 100/333, 112/333, 112/500, 333/500, 500/500?
Wait, does that mean the numbers aren't actually sorted?

Best Answer

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;

comments:

This does exactly what i need :) Thank you ikegami !