0

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.

  • 1
    Please post the final output you're expecting. – ThisSuitIsBlackNot Jan 10 '14 at 16:34
  • What if path 2 was `100 -> 112 --> 400 --> 500`. Is that possible? – ikegami Jan 10 '14 at 16:46
  • I have changed your question so that it is more readable. Please make sure it is correct – Borodin Jan 10 '14 at 18:03
  • 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`? – Borodin Jan 10 '14 at 18:13
  • @ikegami Those numbers are routers. yes it's possible. These are just routing paths to a specific destination. So 100(start) and 500(end) will always be there. But one may experience path changes to reach the destination.The paths could be unlimited because i have a new one per update. For example, i have over 1000 now. I just gave 1 to 5. So what i want to do i, record unstable routers those which frequently disappear and appear in paths. So at the end, the routers with most (count) could be flagged as troubled devices because they flap (ON AND OFF). Just statistically. –  Jan 10 '14 at 18:26
  • @ThisSuitIsBlackNot a table with | Number | Occurrence | –  Jan 10 '14 at 18:29
  • @Borodin does my reply to Ikegami and ThisSuitIsBlackNot answer your question ? But i do not want to store as your example. Just the numbers that exist in one that doesn't exist in the other not in pairs but each should have it's record and counter(occurence) –  Jan 10 '14 at 18:31
  • 1
    Wait, does that mean the numbers aren't actually sorted? – ikegami Jan 10 '14 at 18:33

1 Answers1

1

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;
ikegami
  • 367,544
  • 15
  • 269
  • 518