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