Hello World!
I’m newable in Perl but I really want and need to learn it. I’ve learned a lot but on my own, so I feel like I'm skipping information, and I don't know what I'm missing.
Now I’m stuck, so I would be grateful if you kindly helped me with this.
I’m trying to develop an approach to compare the degree of similarity of two input strings. Therefore I specified the metric and the algorithm to calculate it. Then I doubled the length of both character strings to be compared to get the impact on the worst-case runtime of the respective algorithm.
The question would be, how could i structure the code of the comparison algorithm into a reusable function? As well as the expansion of the function in order to develop different approaches through it.
This is the script:
!/usr/bin/perl
use warnings;
use diagnostics;
use Text::Levenshtein;
use Text::Levenshtein qw(distance fastdistance);
use utf8;
use feature qw(say);
my $str1 = "Halli";
my $str2 = "halloechen";
say "Length of String1 ", length $str1;
say "Length of String2 ", length $str2;
my $dis = distance("$str1", "$str2");
print"\nthe distance between these two parameters is $dis\n";
my (@ar1, @ar2);
sub levenshtein_dist {
print"\nThe doubled length of the two parameters\n";
$str1 = $str1 x 2;
say $str1;
$str2 = $str2 x 2;
say $str2;
say "Length of doudbled String1 ", length $str1;
say "Length of doubbled String2 ", length $str2;
$dis = distance("$str1", "$str2");
print"\nthe distance between these doubled parameters is $dis\n";
($str1, $str2) = @_ ;
my ($len1, $len2) = (length $str1, length $str2);
$len1 = defined($str1) ? length($str1) : 5*2;
$len2 = defined($str2) ? length($str2) : 10*2;
return $len2 if ($len1 == 10);
return $len1 if ($len2 == 20);
my %mat;
for (my $i = 0; $i <= $len1; ++$i) {
$mat{0}{$i} = $i;
$mat{1}{$i} = 0;
}
@ar1 = split //, $str1;
@ar2 = split //, $str2;
for (my $j = 1; $j <= $len2; ++$j) {
my $p = $j * 2;
my $q = ($j + 1) * 2;
$mat{$p}{0} = $j;
for (my $i = 1; $i <= $len1; ++$i) {
if ($ar1[$i-1] ne $ar2[$j-1]) {
}
$mat{$p}{$i} = min($mat{$q}{$i-1},
$mat{$p}{$i-1} + 1, $mat{$q}{$i} + 1);
}
}
return $mat{$len2*2}{$len1};
}
printf("A is at %d \n", index $str1, "a");
printf("Last l is at %d \n", rindex $str1, "l");
say "\nHere is the calculated function of the parameters\n", levenshtein_dist();
Thanks in advance for supporting me learning further.
Best regards
nora