473,407 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

reusable function

3
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
Oct 26 '22 #1
0 13036

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Axter | last post by:
I'm wondering about the practical use of dynamic_cast in reusable or generic code. I'm currently working on a smart pointer that can be used on vector and other STL containers. See following...
27
by: Matt Kruse | last post by:
Since this topic has come up several times in other threads, I thought I'd make a separate thread and gather opinions from (hopefully) a more varied range of newsgroup participants. What are...
8
by: Matt Kruse | last post by:
I've found that under some circumstances, some code that I've been using to find an object's coordinates with respect to the viewport does not behave correctly. Is there a function that has been...
3
by: Armen Rizal | last post by:
Hello all, Is there anybody know where I can find reusable pl/pgsql samples or function library ? Thanks, Armen
1
by: Nacho | last post by:
Hello.. I have one problem with my reusable function to validate date.. I want to use this function to all my web project no validate date, but It's works fine if I have one textbox to...
1
tekninja
by: tekninja | last post by:
I wanted to write a function to take the tedium out of dynamically determining the total number of elements in an array. Within the main method this is easy as I simply use: sizeof( <array name>...
2
by: fpennisi | last post by:
Hi all I have started writing a web app in Visual Studio 2005 (using c#). I am new to both C# and Visual Studio 2005. I will start by telling you what I need and then how I would have done it...
5
by: mad.scientist.jr | last post by:
According to http://www.quirksmode.org/js/forms.html you need to look through a radio button's members to find the value: I tried writing a reusable function to return the value, see code at:...
0
by: lanliddd | last post by:
Lancet is a comprehensive, efficient, and reusable util function library of go. Inspired by the java apache common package and lodash.js. Go version Release GoDoc Go Report Card codecov License ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.