PERL/C2/for-for-each-loops/English-timed
From Script | Spoken-Tutorial
|
|
---|---|
00.01 | Welcome to the spoken tutorial on for and foreach Loops in Perl
|
00.06 | In this tutorial, we will learn about: for loop in Perl and |
00.11 | foreach loop in Perl
|
00.13 | I am using Ubuntu Linux12.04 operating system and Perl 5.14.2
|
00.21 | I will also be using the gedit Text Editor.
|
00.25 | You can use any text editor of your choice.
|
00.29 | You should have basic knowledge of Variables and Comments in Perl.
|
00.33 | If not, please go through the relevant spoken tutorials on the spoken tutorial website.
|
00.40 | Perl provides a mechanism by which we can check a condition repeatedly for various values. This is done using loops.
|
00.49 | There are various types of loops in Perl;
|
00.52 | for loop
|
00.53 | foreach loop
|
00.54 | while loop &
|
00.55 | do-while loop
|
00.56 | In this tutorial, we'll cover for and foreach loop.
|
01.01 | for loop in Perl can be used to execute a piece of code for a certain number of times.
|
01.07 | The syntax of for loop is as follows: |
01.10 | for space open bracket variable initialization semicolon condition semicolon increment |
01.20 | close bracket Press Enter
|
01.22 | Open curly brackets
|
01.24 | Piece of code to be executed multiple times
|
01.28 | Close curly brackets
|
01.30 | Now let us look at an example of a for loop.
|
01.33 | Open the Terminal and type; gedit forLoop.pl space & (ampersand)
|
01.42 | and press Enter
|
01.43 | This will open the forLoop.pl file in gedit.
|
1.48 | Type the following piece of code;hash exclamation mark slash u s r slash bin slash perl
|
01.58 | Press Enter
|
02.00 | for space open bracket dollar i equals to zero semicolon space dollar i less than or equal to four semicolon space dollar i plus plus close bracket
|
02.18 | space
|
02.19 | Open curly bracket press enter
|
02.21 | typeprint space double quote Value of i colon space dollar I slash n double quote complete semicolon
|
02.35 | And Press Enter now
|
02.36 | And Close curly bracket
|
02.39 | Press Ctrl+S to save the file.
|
02.42 | Let me explain what the for loop does. |
02.46 | The variable i is initialized to zero.
|
02.50 | Next, the condition is checked.
|
02.53 | In this case, the condition is i is less than or equal to 4.
|
02.59 | If this condition is true, the code within the curly bracket will be executed.
|
03.06 | This means the first print statement "Value of i: 0"
|
03.11 | will be displayed on the terminal.
|
03.14 | After this, the variable i is incremented by 1.
|
03.19 | And the for loop condition is checked once again.
|
03.23 | This loop will exit when the value of i becomes greater than 4.
|
03.29 | In this case, the for loop will be executed for i = 0, 1, 2, 3, 4
|
03.38 | which is a total of 5 times.
|
03.41 | Now, switch to the terminal.
|
03.44 | Type the following to check for any compilation or syntax error:
|
03.49 | perl hyphen c forLoop dot pl
|
03.54 | and press Enter.
|
03.56 | Here it displays a message
|
03.58 | forLoop.pl syntax OK
|
04.01 | So, we have no errors.
|
04.03 | Now, let's execute the Perl script by typing perl forLoop dot pl and press Enter.
|
04.12 | The following output will be shown on terminal.
|
04.16 | Now, let us look at the foreach loop.
|
04.19 | If we want to iterate a condition for an array, we can make use of foreach loop.
|
04.25 | The syntax is: foreach space dollar variable space within brackets at the rate array
|
04.34 | space
|
04.35 | open curly bracket
|
04.37 | perform action on each element of an array Press Enter
|
04.42 | Close the curly bracket.
|
04.44 | Please note: We'll cover array, array initialization and defining an array in subsequent tutorials.
|
04.53 | Now let us look at an example of foreach loop.
|
04.57 | Open the Terminal and type gedit foreachLoop dot pl space ampersandand Press Enter
|
05.08 | This will open the foreachLoop.pl file in gedit.
|
05.10 | Type the following piece of code |
05.15 | hash exclamation mark slash u s r slash bin slash perl and Press Enter
|
05.25 | at the rate myarray is equal to open bracket ten comma space twenty comma space thirty close bracket semicolon
|
05.39 | press enter
|
05.41 | foreach space dollar var space open bracket at the rate myarray close bracket space |
05.52 | Open curly bracket press enter and type
|
05.56 | print space double quote Element of array is colon dollar var space slash n double quote complete semicolon
|
06.13 | Press Enter and Close curly bracket
|
06.17 | Press ctrl+s to save the file.
|
06.21 | Let me explain what this code does. An array myarray is declared.
|
06.28 | It has 3 elements 10, 20 and 30.
|
06 .33 | In each iteration of foreach loop dollar var will contain the single element of an array
|
06.41 | foreach keyword will repeat this loop for each element of an array.
|
06.48 | That is, the code within the curly bracket will be executed for each myarray element.
|
06.56 | Back-slash n will place the prompt on a new line.
|
07.01 | This means the first element '10' will be displayed on the terminal. |
07.07 | Then 20 and so on, till all the elements are printed.
|
07.12 | This loop will exit after printing all the elements in myarray.
|
07.17 | Now, switch to terminal and type the following to check for any compilation or syntax error.
|
07.24 | type perl hyphen c foreachLoop dot pland press Enter.
|
07.32 | The following line will be shown on terminal
|
07.36 | There are no compilation or syntax errors.
|
07.39 | So let us execute the Perl script.
|
07.41 | Type perl foreachLoop dot pl and press Enter
|
07.49 | The following output will be shown on terminal. |
07.54 | So, that's it about for loop and foreach loop.
|
07.57 | Let us summarize.
|
07.59 | In this tutorial, we have learnt -
|
08.03 | for loop and foreach loop in Perl
|
08.06 | using some sample programs.
|
08.08 | Here is an assignment for you -
|
08.10 | Declare a string as 'Spoken Tutorial' and |
08.14 | Print it 5 times |
08.16 | Declare an array of colours as @colorArray = ('red', 'white', 'blue') and |
08.32 | Print the element of an array using foreach loop
|
08.36 | Watch the video available at the following link
|
08.40 | It summaries the Spoken Tutorial project
|
08.44 | If you do not have good bandwidth, you can download and watch it
|
08.49 | The Spoken Tutorial Project Team Conducts workshops using spoken tutorials
|
08.55 | Gives certificates to those who pass an online test
|
09.00 | For more details, please write to contact at spoken hyphen tutorial dot org
|
09.08 | Spoken Tutorial Project is a part of the Talk to a Teacher project
|
09.12 | It is supported by the National Mission on Education through ICT, MHRD, Government of India.
|
09.20 | More information on this Mission is available spoken hypen tutorial dot org slash NMEICT hyphen Intro
|
09.31 | Hope you enjoyed this Perl tutorial.
|
09.34 | This is Amol Brahmankar signing off.
|
09.36 | Thanks for joining.
|