2

I am attempting to compare the edit distance between two arrays. I have tried using Text:Levenshtein.

#!/usr/bin/perl -w
use strict;
use Text::Levenshtein qw(distance);

my @words = qw(four foo bar);
my @list = qw(foo fear);
my @distances = distance(@list, @words);

print "@distances\n";
#results: 3 2 0 3

I however want the results to appear as follows:

2 0 3
2 3 2

Taking the first element of @list through the array of @words and doing the same through out the rest of the elements of @list. I plan on upscaling this to a much larger arrays.

El David
  • 375
  • 2
  • 3
  • 11
  • 1
    In your example, `distance(@list, @words)` is equivalent to `distance('foo', 'fear', 'four', 'foo', 'bar')`, which computes the edit distance between "foo" and "fear", "foo" and "four", "foo" and "foo", and "foo" and "bar". – ThisSuitIsBlackNot Nov 02 '16 at 21:26
  • This makes sense, thank you. – El David Nov 02 '16 at 21:43

2 Answers2

4

I'm not sure to understand exactly what you meant, but I think this is what you expect :

#!/usr/bin/perl -w
use strict;
use Text::Levenshtein qw(distance);

my @words = qw(four foo bar);
my @list = qw(foo fear);

foreach my $word (@list) {
   my @distances = distance($word, @words);
   print "@distances\n";
}
Orabîg
  • 11,718
  • 6
  • 38
  • 58
1

Taking the first element of @list through the array of @words and doing the same through out the rest of the elements of @list.

You just described exactly what you need to do to get the output you would like; loop through the @list array and for each element compute the distance for all elements of the @words array.

Hunter McMillen
  • 59,865
  • 24
  • 119
  • 170