|
|
PERL
Tutorial
PERL
Tutorial Continued
Example 4
|
|
Loop Modifiers
These modify the normal flow of the program.They are as follows
->next
->break
->last
The next operator starts the next iteration , it is similar to continue
statement in
C language.
#next.pl
@array1=("one","two","three","four");
foreach $i (@array1)
{
if($i eq "three")
{
next;
}
print "$i";
}
The eq operator is a string relational (equality) operator similar to
arithmetic
equality operator ==.
|
Example
5
Defining and using subroutines
The sub keyword is used to identify a user-defined function
#subroutines/functions
sub my_sub
{
print "hello";
}
my_sub;
Example 6
Calculating
the factorial of a number which is taken from the keyboard input This
sample program uses subroutine, diamond , arithmetic operators and
while loop and if constructs
#factorial
$input=<>;
sub factorial
{
$s=1;
$r=1;
while ($s <= $input)
{
$r *= $s;
$s++;
}
if($input == 0)
{
$r=0;
}
return $r;
}
print "The result is".$r;
Go To Index
|
|
|