Troubleshooters.Com and Code Corner Present

Steve Litt's Perls of Wisdom:
Simple Perl String and Numeric Manipulation Handling
(With Snippets)

Copyright (C) 2003 by Steve Litt


Debug like a Ninja


CONTENTS Much of any computer language concerns manipulation and comparison of strings and numbers. Perl has the usual complement of functions and operators for both. Note that much Perl string manipulation is performed with regular expressions, especially if performance is not an issue. Regular expressions are covered in their own document. This document details non-regex string handling.

Numeric Operators

Numeric manipulation is arithmetic. I won't give you Perl's order of precidence -- I don't know it myself. I use parentheses liberally, ensuring the intended calculation. Generally speaking, if you understand C or Java arithmetic, you understand Perl's:

Operator
Functionality
Example
$var contains
=
Assignment
$var = 8;
8
+
Addition
$var = 8 + 3;
11
-
Subtraction
$var = 8 - 3;
5
*
Multiplication
$var = 8 * 3;
24
/
Fractional Division
(5/2 is 2.50, not 2)
$var = 8 / 3;
2.66666666666667
%
Modulus
$var = 8 % 3
2

Whole number division
$var = (8-(8%3))/3
2

Perl's numeric relational operators are similar to C's:

Operator
Functionality
Example
>
Numeric greater than
if($var > 8)
>=
Numeric greater than or equal
if($var >= 8)
<
Numeric less than
if($var < 8)
<=
Numeric less than or equal if($var <= 8)
==
Numeric equality
if($var == 8)

String Manipulation: dot, length(), substr(), index() and rindex()

Use the dot operator to concatinate 2 strings:
$var = "George" . " " . "W." . " " . "Bush";
In the preceding, $var now contains "George W. Bush".

You often need the length of a string. Use the length() function to get that.

The opposite of concatination is truncation. Personally, I do most truncation with regular expressions, which are discussed in a different document. However, you can also use the substr().substr() is faster than regular expressions, so if you're truncating in a tight loop, you might want to usesubstr(). Its syntax is as follows:
substr EXPR,OFFSET,LENGTH,REPLACEMENT
LENGTH and REPLACEMENT are optional. Personally, I seldom use REPLACEMENT because it's clearer to replace text with a concatenation after truncation. Here are some ways you can use substr() to truncate from the right and from the left of a string. In the following examples, imagine that:
$string = "Perl Programmer":

Task
Statement
Value of $var
Remove 3 characters from the front of the string
$var = substr($string, 3);
"l Programmer"
Remove all but the 3 final characters from the string
$var = substr($string", -3); "mer"
Remove the 3 final characters from the string $var = substr($string, 0, - 3);
"Perl Program"
Remove all but the first 3 chaaracters from the string
$var = substr($string, 0, 3);
"Per"

To remove specific text, you can use the index()and rindex() functions to find the offset of the start of a substring, and then plug that number into the OFFSET or LENGTH arguments of substr(). Information on length(), substr(), index() and rindex()are contained in their respective man pages.

String Comparison Operators

Perl's string relational operators are distinct from its numeric relational operators:

Operator
Functionality
Example
gt
String greater than
if($var gt "cccc")
ge
String greater than or equal
if($var ge "cccc")
lt
String less than
if($var lt "cccc")
le
String less than or equal if($var le "cccc")
eq
String equality
if($var eq "cccc")



 [ Troubleshooters.com| Code Corner | Email Steve Litt ]

Copyright (C) 2003 by Steve Litt --Legal