Perl Part II
Perl Part II
Array Variables
Perl enables you to store lists in special variables designed for that purpose. These
variables are called array variables (or arrays for short).
For Example,
Another example
$x = $array[0];
@array = (1,2,3);
$len = @array;
($len, now has the value 3, which is the length of the list
currently assigned to @array.)
2
The syntax for split is
where
string is the character string to split
Example 1:
$string = “words::separated::by::colons”;
Example 2:
3
("Hello", "wild", "windy", "world")
Example 3
#!/usr/bin/perl
For Example,
$line = “This is my line”;
chop($line);
4
After chop is called, the value of $line becomes,
"This is my lin"
The chop function is used to remove the newline character from
strings read from <STDIN>. When a string is read from
<STDIN> the newline character is stored with the string.
Example,
$name = <STDIN>;
This reads a name, including the newline character into $name. We can
remove the newline character using chop:
chop($name);
It is a very common mistake for beginners to forget to
remove the trailing newline and as a result get a comparison
failure.
#!/usr/bin/perl
print("Enter your name: ") ;
$name = <STDIN>;
print("hello ", $name, " nice to meet
you!") ;
5
chop($name);
print("\n\nAfter the axeman!!\n") ;
print("hello", $name, " nice to meet you!\n")
;
After using the chop function, the newline character has been
removed and the output is as expected on a single line.
Comparison Operators
Note that the ASCII codes of the uppercase letters are less than
the ASCII codes of the lowercase letters. This means for
example that the character 'Z' "is less than" the character 'a'.
6
The ASCII code for uppercase letters range from 65 ('A') to 90
('Z') and for lowercase from 97 ('a') to 122 ('z').
7
String comparison examples:
String Concatenation
The string-concatenation operator, . , joins two strings together.
For Example,
$newstring = “potato”.”head”;
8
Conditional Statements
The if Statement
if (expr)
{
statement_block
}
For example,
if ($number >0 )
{
print(“The number is not zero.\n”);
}
Another example,
if ($number == 21)
{
print(“The number is 21\n”);
}
A final example,
if ( $word eq “hello”)
{
print(“You entered hello.\n”);
}
Note we use eq to test for string equality.
9
The syntax for this statement is:
if (expr)
{
statement_block_1
}
else
{
statement_block_2
}
#!/usr/bin/perl
10
The if – elsif – else Statement
The syntax for this statement is:
if (expr_1)
{
statement_block_1
}
elsif (expr_2)
{
statement_block_2
}
elsif (expr_3)
{
statement_block_3
}
else
{
default_statement_block
}
11
The while loop
A while loop allows you repeat one or more Perl statements.
The syntax for the while loop is:
while ( expr )
{
statement_block
}
For example, the following program is an implementation of a
simple guessing game where the user is asked to guess the
"magic" word known only to the program:
#!/usr/bin/perl
chop $guess ;
12
Perl provides the last statement, (equivalent to break in
C, to allow you break out of a loop. For example, in the
guessing program above, the program will loop until the
user makes a correct guess i.e. potentially an endless
loop. The following version uses a last statement to
solve this problem and only allows 3 guesses:
#!/usr/bin/perl
13
The until Statement
There is a slight difference between the while and the
until statement, namely,
until( expr )
{
statement_block
}
Example 1:
$count = 0 ;
until($count == 5)
{
print(“still in the until loop.\n”);
$count ++;
}
print(“$count is now 5.\n”);
14
Example 2:
#!/usr/bin/Perl
15
The for Statement
The syntax of the for statement is similar to that of C:
16
Example 1:
#!/usr/bin/perl
Example 2:
Read a line of text, break it into words and print
the words out on separate lines. Assume any of the
following characters may be used to separate
words: blank, ,?"':;. A range of separators can be
passed to split by enclosing them in [].
#!/usr/bin/perl
17