Broad Network


Perl String Manipulation

Handling Strings in Perl – Part 3

Perl Course

Foreword: In this part of the series, I explain how to manipulate strings in Perl. The sections covered are: comparing strings, reversing a string, joining strings and splitting a string.

By: Chrysanthus Date Published: 23 Oct 2015

Introduction

This is part 3 of my series, Handling Strings in Perl. In this part of the series, I explain how to manipulate strings in Perl. The sections covered are: comparing strings, reversing a string, joining strings and splitting a string. You should have read the previous parts of the series before reaching here, as this is a continuation.

Extension of the Alphabet Series to Other Characters
In the dictionary words are written in alphabetical order. In Perl a word beginning with D is considered to be greater than a word beginning with B. In Perl there is also the issue of case sensitivity. In Perl, b is greater than B; that is, lowercase letters come after uppercase letters. In Perl ‘A’ is greater than 9. In Perl 9 is greater than 0.

You can now imagine a Perl dictionary consisting of alphanumeric words. In this situation, the alphanumeric series order is, 01—9AB –Zab—z. The numbers come first before the uppercase letters and then the lowercase letters. What comes after in the series is considered to be greater than what comes before. String comparison in Perl uses this order.

The cmp Operator
This operator returns –1 if the left argument is stringwise less than the right argument. It returns 0 if the two arguments are stringwise equal. It returns 1 if the left argument is stringwise greater then the right argument. Read and try the following code:

use strict;

    print "alphabetical" cmp "order", "\n";
    print "bcde"<=> "bcde", "\n";
    print "man" cmp "MAN", "\n";

The output is:

-1
0
1

To use this in a condition, remember that –1 and 1 are equivalent to true, while 0 is equivalent to false.

Case Insensitive Comparison
The lc function will return a string in which all uppercase characters have been changed to lowercase. The original string remains unchanged. So, to do case insensitive comparison, you can use lowercase versions of the strings. Try the following code:

use strict;

    my $str1 = "book";
    my $str2 = "Booking";

    print lc($str1) cmp lc($str2);

The output is –1.

The reverse Function
This function has two syntaxes, which are:
   
    reverse VARIABLE
    reverse LIST

If the argument is a string scalar variable, the characters are reversed and returned. The original string remains the unchaged. Read and try the following code:

use strict;

    my $str = "a b c d e";
    my $ret = reverse($str);
    print $ret, "\n";
    print $str, "\n";

The output is:

e d c b a
a b c d e

If the argument to the function is an array (list) of strings, each element is reversed and the order of the elements are also reversed. The original array remains unchanged. Read and try the following code:

use strict;

    my @arr = ("one", "two", "three");
    my $ret = reverse(@arr);
    print $ret, "\n";
    print @arr, "\n";

The output is:

    eerhtowteno
    onetwothree

The above situations are scalar context (receiving variable is scalar).  In list context, the order in which the elements were, is reversed but each element is not reversed. The original array remains unchanged. Read and try the following code:

use strict;

    my @arr = ("one", "two", "three");
    my @arrRet = reverse(@arr);
    print @arrRet, "\n";
    print @arr, "\n";

The output is:

   threetwoone
   onetwothree

Joining Strings
The syntax is:

    join EXPR,LIST

The function joins the separate strings of LIST into a single string with fields separated by the value of EXPR (in quotes), and returns that new string. Try the following code:

use strict;

    my $ret = join(', ', "one", "two", "three");

    print $ret;

The output is:

    one, two, three

You can use variables in place of the list element literals. For the join function, EXP cannot be a regex (pattern).

The split Function
This function splits a big string into a number of smaller strings. If the receiving variable for the return value is scalar, the number of small strings is returned; if it is an array, the sub strings are return to be elements of the array. So, we say, the function splits the string into a list of strings and returns the list in list context, or the size of the list in scalar context. The original string stays unchanged. The syntaxes are:

    split
    split /PATTERN/
    split /PATTERN/,EXPR
    split /PATTERN/,EXPR,LIMIT

The first syntax has no argument and so the string to split is $_  and the separator used in the spit is the space. Read and try the following code:

use strict;

    $_ = "one two three";
    my @ret = split();
    print $ret[0], "\n";
    print $ret[1], "\n";
    print $ret[2], "\n";

The output is:

one
two
three

In the second syntax, PATTERN is the separator and has to be within two forward slashes. /PATTERN/ can also be a regular expression. For this syntax, the string to split is still, $_. In the following code, the separator is comma plus space:

use strict;

    $_ = "one, two, three";
    my @ret = split(/, /);
    print $ret[0], "\n";
    print $ret[1], "\n";
    print $ret[2], "\n";

The output is:

one
two
three

The third syntax is the case where EXPR is the string variable of your choice and not $_. Read and try the following code:

use strict;

    my $str = "one, two, three";
    my @ret = split(/, /, $str);
    print $ret[0], "\n";
    print $ret[1], "\n";
    print $ret[2], "\n";

For the fourth syntax, LIMIT is the number of sub-strings you want. For example, if there are four separators, meaning the string should be split into five sub-strings, the number (LIMIT) of 1 will return the entire string, the number 2 will use the first occurrence of the separator to return two sub-strings, the number 3 will return three sub-strings using the first and second occurrences of the separator, and so on. Read and try the following code:

use strict;

    my $str = "one, two, three, four, five";
    my @ret = split(/, /, $str,3);
    print $ret[0], "\n";
    print $ret[1], "\n";
    print $ret[2], "\n";

The output is:

one
two
three, four, five

That is it for this part of the series. We take a break here and continue in the next part.

Chrys

Related Links

Perl Basics
Perl Data Types
Perl Syntax
Perl References Optimized
Handling Files and Directories in Perl
Perl Function
Perl Package
Perl Object Oriented Programming
Perl Regular Expressions
Perl Operators
Perl Core Number Basics and Testing
Commonly Used Perl Predefined Functions
Line Oriented Operator and Here-doc
Handling Strings in Perl
Using Perl Arrays
Using Perl Hashes
Perl Multi-Dimensional Array
Date and Time in Perl
Perl Scoping
Namespace in Perl
Perl Eval Function
Writing a Perl Command Line Tool
Perl Insecurities and Prevention
Sending Email with Perl
Advanced Course
Miscellaneous Features in Perl
Perl Two-Dimensional Structures
Advanced Perl Regular Expressions
Designing and Using a Perl Module
More Related Links
Perl Mailsend
PurePerl MySQL API
Perl Course - Professional and Advanced
Major in Website Design
Web Development Course
Producing a Pure Perl Library
MySQL Course

BACK NEXT

Comments

Become the Writer's Fan
Send the Writer a Message