in reply to Compare each array element to the rest, sequentially
It's not clear what you want to achieve here... Did I understand it right that you want to build a matrix of distances? Then you can just use two nested foreach loops:
use strict; use warnings; use Data::Dumper; use Text::Levenshtein qw(distance); my $file=$ARGV[0]; my @all_ids=(); open my $in, "<", $file or die "Failed to open $file: $!"; while(<$in>) { chomp $_; push @all_ids, $_; } close $in; my @matrix; foreach my $first( @all_ids ) { my @row; foreach my $second( @all_ids ) { push @row, distance( $first, $second ); }; push @matrix, \@row; }; print Dumper(\@matrix);
This leaves room for optimization, of course.
P.S. use strict; use warnings; . It's 2017 already!
|
|---|