PERL/C2/for-for-each-loops/English
Title Of Script: for & foreach Loops in Perl
Author: Amol Brahmankar
Keywords: Loops in Perl, for loop in perl, foreach in perl, video tutorial.
|
| ||
Slide | Welcome to the spoken tutorial on for and foreach Loops in Perl | ||
Slide: Learning Objectives | In this tutorial, we will learn about:
for loop in Perl and foreach loop in Perl | ||
Slide: System Requirements | I am using Ubuntu Linux12.04 operating system and Perl 5.14.2
| ||
Slide: Prerequisites | You should have basic knowledge of Variables and Comments in Perl.
| ||
Slide | Perl provides a mechanism by which we can check a condition repeatedly for various values. This is done using loops. | ||
Slide | There are various types of loops in Perl;
for loop foreach loop while loop & do-while loop
| ||
Slide | for loop in Perl can be used to execute a piece of code for a certain number of times. | ||
Slide
{ Piece of code to be executed multiple times } |
The syntax of for loop is as follows:
|
Now let us look at an example of a for loop. | |
Switch to the Terminal and type
gedit forLoop.pl & and press Enter. |
Open the Terminal and type;
gedit forLoop.pl space & (ampersand) and press Enter | ||
Point to the filename forLoop.pl in the Titlebar of gedit. | This will open the forLoop.pl file in gedit. | ||
Type the piece of code and save the file
print “Value of i: $i\n”; } |
Type the following piece of code;
space Open curly bracket press enter , type print space double quote Value of i colon space dollar I slash n double quote complete semicolon
now ANd Close curly bracket
| ||
Press ctrl+s | Press Ctrl+S to save the file. | ||
Highlight $i=0 | Let me explain what the for loop does.
| ||
Highlight $i<=4 | Next, the condition is checked.
In this case, the condition is i is less than or equal to 4.
| ||
Highlight print | This means the first print statement
“Value of i: 0” will be displayed on the terminal. | ||
Highlight $i++ | After this, the variable i is incremented by 1. | ||
Highlight $i<=4 | And the for loop condition is checked once again.
| ||
In this case, the for loop will be executed for i = 0, 1, 2, 3, 4
| |||
Switch to terminal | Now, switch to the terminal. | ||
perl -c forLoop.pl
|
Type the following to check for any compilation or syntax error:
| ||
Highlight the below line on terminal;
forLoop.pl syntax OK |
Here it displays a message
forLoop.pl syntax OK
| ||
Execute perl script
|
Now, let's execute the Perl script by typing
| ||
Highlight/point to the output of the perl script on terminal
Value of i: 0 Value of i: 1 Value of i: 2 Value of i: 3 Value of i: 4 |
The following output will be shown on terminal.
| ||
Now, let us look at the foreach loop. | |||
Slide | If we want to iterate a condition for an array, we can make use of foreach loop. | ||
Slide
perform action on each element of an array } |
The syntax is:
Close the curly bracket.
| ||
Now let us look at an example of foreach loop. | |||
Switch to the Terminal and type
|
Open the Terminal and type
| ||
Point to the filename foreachLoop.pl in the Titlebar of gedit. | This will open the foreachLoop.pl file in gedit. | ||
#!/usr/bin/perl
print “Element of array is: $var \n”; }
|
Type the following piece of code
press enter foreach space dollar var space open bracket at the rate myarray close bracket space
press enter and type
Close curly bracket | ||
Press Ctrl+S | Press ctrl+s to save the file. | ||
Highlight
@myarray = (10, 20, 30); |
Let me explain what this code does.
It has 3 elements 10, 20 and 30. | ||
Highlight $var | In each iteration of foreach loop dollar var will contain the single element of an array | ||
Highlight foreach $var (@myarray) | foreach keyword will repeat this loop for each element of an array. | ||
Highlight print “Element of array is:$var \n”; | That is, the code within the curly bracket will be executed for each myarray element. | ||
Highlight \n | Back-slash n will place the prompt on a new line. | ||
This means the first element '10' will be displayed on the terminal.
| |||
Highlight foreach | This loop will exit after printing all the elements in myarray. | ||
Switch to terminal
|
Now, switch to terminal and type the following to check for any compilation or syntax error.
type perl hyphen c foreachLoop dot pl
| ||
Highlight the below line on terminal;
foreachLoop.pl syntax OK |
The following line will be shown on terminal | ||
Execute perl script
|
There are no compilation or syntax errors.
So let us execute the Perl script. Type
| ||
Highlight the output of the perl script on terminal
Element of an array is: 10 Element of an array is: 20 Element of an array is: 30 |
The following output will be shown on terminal.
| ||
So, that's it about for loop and foreach loop. | |||
Slide : Summary | Let us summarize.
In this tutorial, we have learnt - for loop and foreach loop in Perl using some sample programs. | ||
Assignment | Here is an assignment for you -
| ||
About the Project | Watch the video available at the following link
download and watch it | ||
Spoken Tutorial Workshops | The Spoken Tutorial Project Team
test
contact at spoken hyphen tutorial dot org | ||
Acknowledgment | Spoken Tutorial Project is a part of the Talk to a
Teacher project
Education through ICT, MHRD, Government of India.
| ||
Hope you enjoyed this Perl tutorial.
This is Amol Brahmankar signing off.
|