Programming Basics

  Home arrow Programming Basics arrow Perl String Processing

Perl String Processing
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating:  stars stars stars stars stars / 0

    Table of Contents:
  • Perl String Processing
  • The index() Function



    Perl String Processing

    (Page 1 of 2 )

    If you're learning how to use Perl, you may already be aware of its power as a text processing language. It gets that power in a number of ways, not the least of which is its built-in string operators and string functions. This three-part article series will show you some of the many ways in which you can process strings in Perl. It is excerpted from chapter nine of the book Beginning Perl, Second Edition, written by James Lee (Apress; ISBN: 159059391X).

    Perl was created to be a text processing language, and it is arguably the most powerful text processing language around. One way that Perl displays its power in processing text is through its built-in regular expression support that we discussed in Chapter 7. Perl also has many built-in string operators (such as the string concatenation operator.and the string replication operatorx) and string functions. In this chapter we will explore several string functions and one very helpful string operator.

    Character Position

    Before we get started with some of Perl’s built-in functions, we should talk about the ability to access characters in a string by indexing into the string. The numeric position of a character in a string is known as its index. Recall that Perl is 0-based—it starts counting things from 0, and this applies to character indexing as will. So, for this string:

    "Wish You Were Here"

    here are the characters of the string and their indexes:

    character 0: W
    character 1: i
    character 2: s
    character 3: h
    character 4: <space>
    character 5: Y
    character 17: e

    We can also index characters by starting at the right-most character and counting down starting from –1 (much like accessing an array). Therefore, the characters in the preceding string are also known by these indices:

    character -1: e
    character -2: r
    character -3: e
    character -4:H
    character -5: <space>
    character -6: e
    character -18: W

    String Functions

    Perl has many string functions built into the language. We will now discuss several of the most common functions used to process text.

    The length() Function

    To determine the length of a string, we can use the length()function.


    This function returns the number of characters of its argument. If no argument is given,length()returns the number of characters of$_. Here is an example:

    #!/usr/bin/perl -w

    use strict;

    my $song = 'The Great Gig in the Sky';
    print 'length of $song: ', length($song), "\n";
    # the *real* length is 4:44

    $_ = 'Us and Them';
    print 'length of $_: ', length, "\n";
    # this one is 7:40

    Running this code produces the following:

    $ perl
    length of $song: 24
    length of $_: 11

    More Programming Basics Articles
    More By Apress Publishing

    blog comments powered by Disqus


    - The Transliteration Operator in Perl
    - Perl String Processing Functions
    - Perl String Processing
    - Control Flow Constructs: Loops Conclusion
    - Loop Control Constructs
    - Control Flow Constructs: the For and Foreach...
    - Loops and Control Flow Constructs
    - Expression Modifiers for Perl Control Flow C...
    - Logical Operators and Control Flow Constructs
    - Comparing Strings with Control Flow Construc...
    - Perl Operators and Control Flow Constructs
    - Control Flow Constructs
    - More Time Manipulation with PHP
    - Validating and Manipulating Dates with PHP
    - Using the Date Constructor in PHP

    Developer Shed Affiliates


    © 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap