join
functionjoin
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
|
|||
#!/usr/bin/perl -w # Name: Mark Tucker # Assignment: Lab08 Example 00 # Description: join function examples #========================================================================== @greek = qw( alpha beta delta epsilon zeta eta iota gamma theta omega ); # Join array elements together with the * character $string_one = join('*', @greek); print "String 1:\n$string_one\n\n"; # Join array elements together in one continuous string $string_two = join('', @greek); print "String 2:\n$string_two\n\n"; # Join array elements together with a variable $join_str = ':::'; $string_three = join($join_str, $greek[3], $greek[7], $greek[1], $greek[0]); print "String 3:\n$string_three\n\n"; exit; # DONE |
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] |
.
). This can be used to contatenate strings in
variable assignment or in other functions such as a
print
statment.
lab08_1.pl
|
|||
#!/usr/bin/perl -w # Name: Mark Tucker # Assignment: Lab08 Example 01 # Description: Concatenation examples #========================================================================== @greek = qw( alpha beta delta epsilon zeta eta iota gamma theta omega ); # concatenation during variable assignment $alphabeta = $greek[0] . $greek[1]; print "Alpha/Beta is $alphabeta\n"; # concatenation in a print statment. print "This string was written on ". "multiple lines but will ". "print only one.\n"; exit; # DONE |
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] |
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
|
|||
#!/usr/bin/perl -w # Name: Mark Tucker # Assignment: Lab08 Example 02 # Description: Substring examples #========================================================================== # assign value for a string $test_string = 'abcdefghijklmnopqrstuvwxyz'; # extract a substring and print to the screen $newsubstr = substr($test_string, 3, 5); print "New substring is '$newsubstr'\n"; # extract a substring and print to the screen using variables # to define the substring $strlngth = 7; $offset = 9; $second_substr = substr($test_string, $offset, $strlngth); print "Second substring is '$second_substr'\n"; exit; # DONE |
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] |
length
Functionlength
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
|
|||
#!/usr/bin/perl -w # Name: Mark Tucker # Assignment: Lab08 Example 03 # Description: Substring and length() examples #========================================================================== # assign value for a string $str_one = 'Perl is great!'; $str_two = 'I love Unix!!'; $str_three = 'Computers are evil'; # returning the length and saving in a variable. $length_two = length($str_two); print "String two is $length_two characters long.\n"; # Returning the length in a print statement using concatenation print "String one is " . length($str_one) . " characters long.\n"; # Using the length function inside a substring function $sl = 4; $offset = length($str_three) - 4; $last_four = substr($str_three, $offset, $sl); #an equivalent statement $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; # DONE |
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] |
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
|
|||
#!/usr/bin/perl -w # Name: Mark Tucker # Assignment: Lab08 Example 04 # Description: Case manipulation #========================================================================== # get the user's name print "Please enter your name:\n"; chomp($myname = <STDIN>); # upper case the first character of the string print "Capitalize: " . ucfirst($myname) . "\n"; # upper case the entire string print "Upper case: " . uc($myname) . "\n"; # reassing $myname as an upper case string $myname = uc($myname); # lower case the first character of the string print "Lower first: " . lcfirst($myname) . "\n"; # lower case the entire string print "Lower case: " . lc($myname) . "\n"; exit; # DONE |
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