Week 3 - February 2, 1999

3.1 Scalar Operators and Function

3.2 Special String Operators: tr///, s/// and m//
3.3 Array Functions
3.4 Associative Arrays Functions

3.1 Scalar Operators and Function

An operator manipulates data objects called operands. The operands can be either strings, numbers, or combinations of both. Data objects can be manipulated in a number of ways by the large numbers of operators provided by Perl. Most of the Perl operators are borrowed from C, although Perl has some additional operators of its own. In Perl 5 you can use functions as operators if the parantheses are omitted.

3.1.1 Assignment Operators

Operator Example Meaning
= $var = 5; Assign 5 to $var
+= $var += 3; Add 3 to $var and assigns result to $var
-= $var -= 7; Substract 7 from $var and assigns result to $var
*= $var *= 4; Multiply $var by 4 and assigns result to $var
/= $var /= 2; Divide $var by 2 and assigns result to $var
**= $var **= 2; Square $var and assigns result to $var
%= $var %= 3; Divide $var by 3 and assign reminder to $var
.= $str .= "ing"; Concatenate "ing" to $srt and assign result to $str
x= $str x= 20; Repeat value of $str 20 times and assign result to $str

3.1.2 Relational Operators

Numeric
Operator
String
Operator

Meaning
> gt Greater than
>= ge Greater than or equal to
< lt Less than
<= le Less than or equal to

3.1.3 Equality Operators

Numeric
Operator
String
Operator

Returns
== eq true if the operators are equal
!= ne true if the the operators are not equal
<=> cmp 1 if first operator is greater than second one,
0 if they are equal, and
-1 if first operator is less than the second one.

3.1.4 Arithmetic Operators

Operator Example Meaning
+ $x + $y Addition
- $x - $y Substraction
* $x * $y Multiplication
/ $x / $y Division
% $x % $y Modulus
** $x ** $y Exponential
++ $x++ or ++$x Post or Pre-increment; equivalent to $x = $x + 1
-- $x-- or --$y Post or Pre-decrement; equivalent to $x = $x - 1

Examples: Assume that at the beginning of each of the following examples $x=0 and $y=0.

Example Equivalence Result
$y = ++ $x; at the end, $y = 1 and $x = 1. Equivalent to
$x = $x + 1;
$y = $x;
$y = $x ++; at the end, $y = 0 and $x = 1. Equivalent to
$y = $x;
$x = $x + 1;
$y = -- $x; at the end, $y = -1 and $x = -1. Equivalent to
$x = $x - 1;
$y = $x;
$y = $x --; at the end, $y = 0 and $x = -1. Equivalent to
$y = $x;
$x = $x - 1;

3.1.5 String Operators and Functions

Example Meaning
$str1 . $str2 Concatenate $str1 and $str2.
$str1 x $num1 Concatenate $str1, $num1 times.
substr($str1, $offset,$len) Substring of $str1 at $offset for $len bytes (characters).
index($str1,$str2) Byte (character) offset of $str2 in $str1.
rindex($str1,$str2)
rindex($str1,$str2,$pozition)
Returns the pozition of the last occurence of $str2 in $str1.
If $pozition is specified, start looking there. If $position is
not specified, start at the end of $str1.
length($str) Returns the length in characters of the string.
chr(NUMBER)
chr($num)
Returns the character represented by NUMBER or $num in
the ASCII set. For example, chr(65) is 'A'
lc($str) Returns the lowercase version of $str.
uc($str) Returns the uppercase version of $str.

Example:

3.2 Special String Operators: tr///, s/// and m//

There are 3 main operators that ease our work with strings. They are used to change the content of strings or to indentify various substrings. These operators are tr///, s///, and m//. These operators work on the $_ variable unless you use the =~ operator. You can see the difference in the examples below.

Example.

3.2.1 tr///

This command operates on a hole string at one, converting each individual character in turn. The example above does an uppercase to lowecase translation. To better understand what tr/// is doing, lets call the text between the first two slashes the left hand side (lhs) and that between the last to slashes the right hand side (rhs). Each side represents a set of characters, and each character on the lhs is replaced with the corresponding character from the rhs. The dash (-) is not another character; in this context it acts to expand the A-Z to fill in all the missing characters from A to Z, and likewise for a-z. tr/// leaves characters not found in lhs alone. Thus, in the previous example, nothing happens to punctuation, numbers or characters which are already in lowercase.

Example.

will output

If the number of characters in the lhs is greater than the number of characters in the rhs, the last element of the rhs will use for all unmatched characters from lhs. If the number of characters in the rhs is greater than the number of characters in the lhs, all unmatched characters from rhs will be ignored.

3.2.2 s///

This command is related to tr/// but instead of replacing each character in the lhs with the corresponding one from the rhs it searches through the query variable trying to find a match to the whole lhs and then replaces that with the rhs. For example

will print A sunny day.

Substitutions only occur if the string on the left can be matched. One can say that you can do that easily using the string functions index and substr. This is true, but with those functions you can not do pattern-matching. You can declare the lhs to be a patter, and then replace any occurance with the rhs. The most unrestricted character in patter definition in Perl is the period (.). It will match any single character, except a newline. For example

will print Humidity: 5%. Note that the length of the string on the lhs and rhs don't need to be the same, since the whole left is replaced with the whole right. What happens if instead of 80% we have 9% in the $weather scalar? Well, since it has to match 2 characters, the space before 9% will be also replace, thus

will print Humidity:5%.

There are other metacharacters that we can use to define patterns. \d will match anysingle digit, \w will match any "word" character (a-z, A-Z) and \s will match any white space. There are also antonyms like \D, \W, \S which match anything but digits, words and spaces, respectively. For example

will print

To match a variable number of characters, we use the asterisk (*). It modifies the previous character or metacharacter in the patter to match zero or more characters. For example, do*g will match dog, dg, doog, and dooooooooooog. The expression .* matches any string of any length and \d* mathces zero or more digits. Thus, goind back to our Humidity example, \d*% will alwys prin Humidity: 5% no matter how much is the actual humidity: 9%, 80% or 100%. A modifier similar to * is the question mark (?), which matches zero or one occurences of the character. To use any of the metacharacters (., *, or ?) to identify the character it represents, we have to use a backslash before it. Thus \. will identify a period, \\ matches asingle backslash, \? matches a question mark, and \* matches an asterisk.

Example.

Instead of using the predefined wildcard you can define your own ones. For example, [abc] will match any character in the set between the square brackets (this counts as one character). To make a wildcard that matches "everything except" your list, then use the carat (^) character. Thus [^nN] will match everything except n or N.

We can add additional parametersto the s/// operator. Putting a g after the last slash will cause all occurences of the lhs to be replaced by the rhs rather than just the first one, and i makes the pattern matching case-insensitive.

Example.

will print

3.2.3 m//

This operator works like the s/// operator, but it just matches the lhs in the operand. It returns true if a match was made. All what we presented for s/// works for m// operator. Moreover, m can be omitted, so we can write just

3.3 Array Functions

chop(@array)
chomp(@array)

The chop function chops off the last character in each scalar of the list (@array). The chomp function removes only the last character if it is a newline. For example

will print string 10 tex 2 while

will print string 100 text 20.

grep(EXPR, @array)

The grep function evaluates the expression (EXPR) for each element in the list (@array). The return is either another array consisting those elements for which the expression evaluated is true or a scalar that represents the number of times the expression was true. For example

will store 3 in $count and ("computer","printer","driver") in $found.

join(DELIMITER, @array)

The join function joins elements of an array into a single string and separates each element of the array with a given delimiter, which can be a character or a string. This function is the oposit of the split function.

will print My name is Bogdan.

pop(@array)

The pop function pops off the last element of an array and returns it. The array size is decreased by one.

will print

push(@array, LIST)

The push function pushes values onto the end of an array, increasing the length of the array.

Now @list contains all the elements presented above.

shift(@array)

The shift function shifts off and returns the first element of an array, decreasing the size by one element.Works similar to pop, but instead of operating at the end of the array, it operates at the beginning.

splice(@array, OFFSET, LENGTH, LIST)
splice(@array, OFFSET, LENGTH)
splice(@array, OFFSET)

The splice function removes and replaces elements in an array. OFFSET is the starting position where elements are to be removed. LENGTH is the number of items from the OFFSET position to be removed. LIST consists of new elements that are to replace the old ones. If LENGTH and LIST are ommited, splice will remove all the elements from OFFSET to the end of the string.

will store ("computer","keyboard","driver") in @list.

split(DELIMITER, STRING, LIMIT)
split(DELIMITER, STRING)

The split function splits up STRING by some DELIMITER (white space by default) and returns an array. If the DELIMITER doesn't match a delimiter, split returns the original string. You can specify more than one delimiter using the regular expression metacharacter, []. For example, [ +\t:] represents zero or more spaces, or a tab, or a colon. The LIMIT soecifies the number of fields that can be split. If there are mre than LIMIT fields, the remaining fields will all be part of the last one.

will print

sort(SUBROUTINE @array)
sort(@array)

The sort function sorts and returns a sorted array, If SUBROUTINE is ommited, the sort is in a string comparison order. If SUBROUTINE is specified, the first argument to sort is the name of the subroutine followed by a list of integers. The subroutine returns an integer less than, equal to, or greater than 0. The values are passed to the subroutine by reference and are received by the special variables $a and $b, not the normal @_ array.

will output

Note: There is no comma between subroutine name and the list when calling the sort function.

reverse(@array)

The reverse function reverses the elements in an array, so that if it was descending, now it is ascending. Always the first element becomes the last one, and viceversa. Reverse does not compare elements in an array, just changes the order.

unshift(@array, LIST)

The unshift function adds elements from the LIST at the beginning of the array. It is simmilar to push, but works at the beginning of the string rather then at the end.

3.4 Associative Arrays Functions

keys(%assoc_array)
values(%assoc_array)

The keys function returns an array whose elements are the keys of an associative array.The values function returns an array whose elements are the values of an associative array.

will output

each(%assoc_array)

The each function returns, in random order, a two element array whose elements are the key and the corresponding value of an associative array.

will output

delete $assoc_array{key}

The delete function deletes a value from an associative array. The deleted values is returned if successful. For example, to completely delete the elements of an associative array we can use the following code: