Updated April 5, 2023
Introduction to Perl replace
The Perl replace is one of the regular expression features to replace the values by using some operators, symbols even though we used tr is one of the tools for replacing the string type of characters from one variable to another variable in pair-wise like regular expression will compare and differentiate the string replace and matches while tr mapped each character from the character by character so tr keyword will map the strings by using hash map techniques so it follows the key-value pairs for mapping and replacing the values from one variable to another variable.
Syntax:
In Perl script languages string, characters, numbers, we used different data types with values same and stored them on the variables. We used string type of values means it will compare and replace the values only on the string types like that number will compare other numbers. Here we use the replace feature of Perl script with different scenarios.
use strict;
use warnings;
my $var = "";
$var1=~tr/characters;
---some Perl script logic codes depend upon the requirement---
The above code is the basic syntax for replacing the characters; it may be any type like alphabets, numerical digits, etc. But we want to compare using the “tr” tool or is nothing but similar to regular expressions; the strings or characters are matched from the user inputs.
How does the replace method work in Perl?
We know that regular expressions and other special characters like operators and symbols are mapped to the user inputs. It will translate the characters from one side into another side like that if we use ‘=~’ these operators will be taking two parameters one is the string on the left side of the operator, and regular expression is on the right side so that it will easily compare and replace the values. Like that, we use ‘/,^’ for these operators we compare and replace the string in character by character. If we want to compare the numerical digits ‘\d’ is the option for matches it starts from 0-9 it also matches the digits with one by one, so it is the standardized one.
We also use a tool like ‘tr’ is the translator tool that can be replaced the strings in character by character with the help of mapping techniques like key-value pairs; it will validate each characters using some default operators also we want to replace the fixed characters of the string will use substr() as the replaceable function for the script. When we use substr, it will combine the expression, offset values, and length of the strings; all these are joined together, and it will be called using these function.
Examples
Here are the following examples mention below
Example #1
Code:
use strict;
use warnings;
my $vars = "Welcome To My DOmain hgasdfhg gjhsa 153276 agsdh 27e3 jdsg2368 jweg2386 wjehg32846 jhewg386 hew38468 hewg3264 khewr346 wegr3286 kher8346 kjer78 346 kjwge346 hwkjge23 7";
$vars =~ s/To/Hi/ig;
print $vars;
print substr($vars,5,9);
$vars =~ s/(\d+)/"$1"/ig;
print $vars;
Output:
In the above example, we used the string characters in one separate variable, and we will use some string default methods like substr(); it will be used to find the characters in the strings, and it will be replaceable by other characters. Like that, we used some default character sequences like ~s \d+,$1, and /ig; these are the characters used to segregate the string characters and stored in the separate variable.
Example #2
Code:
use strict;
use warnings;
use 5.010;
my $vars = 'Wleocme To My Domain asdhv 283 jdsghv23ey8 328 jweg 2y3 jwehg 23yr kjwge929 83 jkkweg 234y hwej 923yr kjwegr8 38yr4t jweg838 4 jgds2393984 h gef 38248';
say $vars;
$vars =~ tr/e/j/;
say $vars;
sub demo
{
my $vars1 = shift;
my %vars2 = (
"283" => "super",
"83" => "welcome",
);
my $vars3 = $vars2{$vars1};
if(defined($vars3))
{
return $vars3;
}
return $vars1;
}
sub result
{
$vars =~ s/(\d+)/demo($1)/eig;
print $vars;
}
result();
Output:
In the second example, we used the same operations, but here we used a “tr” tool to replace the characters in the variable strings. But here, we used subroutines to assign the group of statements is combined and joined together as the single routines so that we will use the sub keyword to declare the statements by using if condition we will evaluate the values of the variables, and here we utilize the hash map techniques for to handle the values with key-value pairs. We can call the subroutines with the other subroutines for calling the statements and functional codes to handle the replacements. The shift is one of the keyword and function for removing the variable values; by using this operation, we can remove it and replace the new variable values from the left to right side operations. Mostly we used the array concept for handling the shift operations in the script.
Example #3
Code:
use strict;
use warnings;
use Benchmark;
my $vars = '.' x 2000;
sub demo {
$vars =~ tr/./_/;
$vars =~ tr/_/./;
}
sub demo1 {
$vars =~ s/\./_/g;
$vars =~ s/_/./g;
}
sub demo2 {
$vars =~ s/(\d+)/"$1"/ig;
$vars =~ s/_/./g;
}
my $vars1 = 3000;
timethese ( $vars1, {
'Welcome To My Domain' => \&demo,
'Hi Users Have a Nice Day' => \&demo1,
'hgsdj2783 836e dgsh 286e3 jgdsh23 e6 jwge 372 jgweh 3ey sdjg 2386t jy348r7 j3487 jhger8436 jg87473y jgjre 843t jger8645tjhg4875 gr486 jg3r885t j3y58' => \&demo2
} );
Output:
In the final example, we used the time intervals for replacing the variable values. We took 3000 seconds for iterating the values from the user inputs. Here also we used the same subroutines method to handle the other statements, and additionally, we used the timethese() default function for calculating the time intervals for splitting and replacement of the string characters in the script.
Conclusion
In the concluded part, the replacement is one of the features for correcting and changing the alphabets, characters, and numbers for the needs of user perspectives. Here we used some default methods, keywords, and variables to handle these changes on the script. Additionally, we can use some advanced techniques for replacing the values like time-consuming and sub-routines, etc.; these advanced methods also to handle these replacements.
Recommended Articles
This is a guide to Perl replace. Here we discuss how the to replace method works in Perl and Examples and the codes and outputs. You may also look at the following articles to learn more –