Categories

Tools

bitdefender

1&1 Web Hosting

Example of using string manipulation functions with Perl


This example shows how to use some of the functions for manipulating strings with Perl.

Code:

# Define a variable
my $text = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit";

# Print the value of the variable
print '$text: ' . $text . "\n";

# Create a separator line, repeating to 70 times the character -
$sep = "-" x 70; 
print $sep . "\n";

# Print the length of the string
print '$text have '. length ( $text ) . " chars\n";


# Some string manipulation functions
$temp = $text;

# Replace all vocals with *
$temp =~ tr/aeiou/*****/;
print $temp . "\n";

$temp = $text;
# Encrypting the text
$temp =~ tr/a-z/s-zl-ra-i/;
print $temp . "\n";

# Decipher the text
$temp =~ tr/s-zl-ra-i/a-z/;
print $temp . "\n";

$temp = $text;
# Convert all text to lowercase
$temp = lc( $temp ); 
print $temp . "\n";

$temp = $text;
# Convert all text to uppercase
$temp = uc( $temp ); 
print $temp . "\n";

# Create a list of names
my $list = "LIST" . join qq{\n - }, ':', 'Daisy', 'Tomas', 'Carlos', 'Dany', 'Eric';
print $list . "\n";;

Sample result:

mdmsoft@linux-4nve:~/perl> perl test.pl
$text: Lorem ipsum dolor sit amet, consectetuer adipiscing elit
----------------------------------------------------------------------
$text have 56 chars
L*r*m *ps*m d*l*r s*t *m*t, c*ns*ct*t**r *d*p*sc*ng *l*t
Lrcwp ladfp vrorc dle spwe, urqdwuewefwc svlaldulqy wole
Lorem ipsum dolor sit amet, consectetuer adipiscing elit
lorem ipsum dolor sit amet, consectetuer adipiscing elit
LOREM IPSUM DOLOR SIT AMET, CONSECTETUER ADIPISCING ELIT
LIST:
 - Daisy
 - Tomas
 - Carlos
 - Dany
 - Eric

Posted in Perl by MdmSoft

If our work has been of help, you can help us with a small donation...
Our programmers will thank you!



All information contained in this web site are the property of MdmSoft. The information is provided "as is", MdmSoft will not be liable for any misuse of the code contained in these pages, nor can it be for inaccuracies, grammatical errors or other factors that may have caused damage or lost earnings. MdmSoft is not responsible for the content of comments posted by users.
The examples in this area have the educational and demonstration purposes only, and may be copied only for your reference, but cannot be used for commercial purposes, or for any other purpose, without the express written consent of MdmSoft.
MdmSoft also reserves the right to change, without notice, to your liking this web site, the pages and its sections, and may suspend temporarily or definitely the various services included on this site.
While using this site, you agree to have read and accepted our Terms of Service and Privacy Policy.