Notes: Perl Lab 08
Outline
- Join Function
- Concatenation
- Substrings
- length function
- Case manipulation
-
The join
function
The join
function joins the elements of a list
(array) together as a single string with the individual elements
separated by the specified string. While this appears to be
simililar to the split
function, the first argument
cannot be a pattern, it must be a string.
lab08_0.pl
|
@greek = qw( alpha beta delta epsilon zeta eta iota gamma theta omega );
$string_one = join('*', @greek);
print "String 1:\n$string_one\n\n";
$string_two = join('', @greek);
print "String 2:\n$string_two\n\n";
$join_str = ':::';
$string_three = join($join_str, $greek[3], $greek[7], $greek[1], $greek[0]);
print "String 3:\n$string_three\n\n";
exit;
|
When executed, the script above produces the following output:
|
[mark@iguana perl] ./lab08_0.pl
String 1:
alpha*beta*delta*epsilon*zeta*eta*iota*gamma*theta*omega
String 2:
alphabetadeltaepsilonzetaetaiotagammathetaomega
String 3:
epsilon:::gamma:::beta:::alpha
[mark@iguana perl]
|
-
String Concatenation
In perl, the concatenation operator is the dot character
(.
). This can be used to contatenate strings in
variable assignment or in other functions such as a
print
statment.
lab08_1.pl
|
@greek = qw( alpha beta delta epsilon zeta eta iota gamma theta omega );
$alphabeta = $greek[0] . $greek[1];
print "Alpha/Beta is $alphabeta\n";
print "This string was written on ".
"multiple lines but will ".
"print only one.\n";
exit;
|
When executed, the script above produces the following output:
|
[mark@iguana perl] ./lab08_1.pl
Alpha/Beta is alphabeta
This string was written on multiple lines but will print only one
[mark@iguana perl]
|
-
Substrings
Substrings can be extracted from a string using the
substr
function. The function uses the following
syntax:
substr($string, $offset, $length)
In the above statement $string
would be the text
string from which a substring will be extracted. The
$offset
and $length define which substring of the
original string ($string
) will be returned.
$offset
tells the substr
function
where to begin the substring. If the substring were to begin
with the first character of the string then the value of
$offset
would be 0. If we wished to start the
substring with the third character then the value of
$offset
$length, defines how long, in characters, the
substring will be. $length
should be an integer
value. Some examples below:
lab08_2.pl
|
$test_string = 'abcdefghijklmnopqrstuvwxyz';
$newsubstr = substr($test_string, 3, 5);
print "New substring is '$newsubstr'\n";
$strlngth = 7;
$offset = 9;
$second_substr = substr($test_string, $offset, $strlngth);
print "Second substring is '$second_substr'\n";
exit;
|
When executed, the script above produces the following output:
|
[mark@iguana perl] ./lab08_2.pl
New substring is 'defgh'
Second substring is 'jklmnop'
[mark@iguana perl]
|
-
The length
Function
The length
function simply returns the length of a
text string. It is useful when used in conjunction with the
substring function. Below is a script with some examples of how
to use the length
function:
lab08_3.pl
|
$str_one = 'Perl is great!';
$str_two = 'I love Unix!!';
$str_three = 'Computers are evil';
$length_two = length($str_two);
print "String two is $length_two characters long.\n";
print "String one is " . length($str_one) . " characters long.\n";
$sl = 4;
$offset = length($str_three) - 4;
$last_four = substr($str_three, $offset, $sl);
$last_four_more = substr($str_three, (length($str_three) -4), 4);
print "The last four characters of '$str_three' are '$last_four' or ".
"'$last_four_more'.\n";
exit;
|
When executed, the script above produces the following output:
|
[mark@iguana perl] ./lab08_3.pl
String two is 13 characters long.
String one is 14 characters long.
The last four characters of 'Computers are evil' are 'evil' or 'evil'
[mark@iguana perl]
|
-
Case Manipulation
Perl has several functions for altering the case of a string.
Function | Description |
uc($string) | Returns the value of $string will all
characters converted to upper case |
lc($string) | Returns the value of $string with all
characters converted to lower case |
ucfirst($string) | Returns the value of $string with
just the first character converted to uppercase. All other
charactes in the string are unchanged. |
lcfirst($string) | Returns the value of $string with
just the first character converted to lower case. All other
characters in the string are unchanged. |
Some examples:
lab08_4.pl
|
print "Please enter your name:\n";
chomp($myname = <STDIN>);
print "Capitalize: " . ucfirst($myname) . "\n";
print "Upper case: " . uc($myname) . "\n";
$myname = uc($myname);
print "Lower first: " . lcfirst($myname) . "\n";
print "Lower case: " . lc($myname) . "\n";
exit;
|
When executed, the script above produces the following output:
|
[mark@iguana perl] ./lab08_4.pl
mark tucker
Capitalize: Mark tucker
Upper case: MARK TUCKER
Lower first: mARK TUCKER
Lower case: mark tucker
[mark@iguana perl]
|
last updated: 18 Mar 2012 13:04