Sunday, November 1, 2020

Perl Split | Perl Function | String Functions in Perl | Perl Scripting | Perl Programming

This is a Perl tutorial on Perl Split and other string functions in Perl from the Perl Tutorials. Perl language has many functions for string processing, that are useful for processing HTML and XML. This tutorial explains some important string functions in Perl. First, view the Perl Split and String Functions tutorial. Then read below.

Let us learn Perl scripting with examples. Here is the first Perl script example with Perl function lc (to convert a string to lower case) and Perl function uc (to convert a string to upper case):

# Perl code example
# First, implement Perl basics.
use strict; # Perl use strict finds any error in the Perl code if found, stops running the code.
use warnings; # Perl use warnings gives a warning if there is any problem with the Perl code.
use feature qw(say); # say is better than Perl print statement because it appends a newline automatically.
# Perl my to declare a variable in Perl
my $blog = "Software Testing Space";
# string functions in Perl
# Perl print variable in lowercase
say "The blog name in lower case is ", lc($blog);
# Perl uppercase or Perl uc string function
say "The blog name in upper case is ", uc($blog);

Here is another Perl scripting example with length Perl function, Perl string operations and index Perl function (to find a substring within a Perl string):

# Perl code example
use strict; # Perl use strict
use warnings; # Perl use warnings
# use strict and use warnings help us write the correct Perl code in Perl programming.
use feature qw(say); # better form of Perl print
# Perl basics are addressed above. Now, write the actual Perl code.
my $name = "Inder P Singh"; # string assignment
say "The length of name, including spaces, is ", length($name);
# Perl expression of string multiplication using x string operator
# . is string the string operator for concatenation
say "The repeated name is ". $name x 3;
my $text = "abcdef";
# Perl find by index; The indexes are 0-based, meaning they start from 0.
say "The string de occurs in text at ", index($text, "de");
 #index string function returns -1 if the substring is missing in the string.
say "The string x occurs in text at ", index($text, "x");

Here is another Perl example with join Perl function, reverse Perl function and Perl split:

# Perl code example
use strict;
use warnings;
use feature qw(say);
# Perl my
my $blog = "Software Testing Space";
# An array variable in Perl can store a list of values.
my @letters = ("a".."j");# An array is declared within parentheses as a range.
# Perl print array; a Perl array is 0-based.
say "The first five letters are ", @letters[0..4]; # Print the first 5 elements of the array.
# join string function returns the joined string with a separator.
say "The joined letters are ", join(":",@letters);
# reverse string function returns the string with the characters in reverse order.
say "The reverse of letters is ",  reverse(@letters);
# Based on a pattern, the split function in Perl returns a list of strings.
# The pattern in Perl split is given within a pair of forward slashes.
# The pattern is a single space character below.
say "The words in the blog are ", split(/ /,$blog); # Perl split
# Split command in Perl with another Perl example, using join with Perl split
say "The comma separated words in the blog are ", join(",",split(/ /,$blog)); # Perl split command

Want to learn Perl more and see the above Perl examples run? Then please view my Perl Split | String Functions in Perl tutorial. Thank you.


1 comment: