1 00:00:01 --> 00:00:05 Welcome to the spoken tutorial on for and foreach Loops in Perl 2 00:00:06 --> 00:00:10 In this tutorial, we will learn about: for loop in Perl and 3 00:00:11 --> 00:00:12 foreach loop in Perl 4 00:00:13 --> 00:00:20 I am using Ubuntu Linux12.04 operating system and Perl 5.14.2 5 00:00:21 --> 00:00:24 I will also be using the gedit Text Editor. 6 00:00:25 --> 00:00:28 You can use any text editor of your choice. 7 00:00:29 --> 00:00:32 You should have basic knowledge of Variables and Comments in Perl. 8 00:00:33 --> 00:00:39 If not, please go through the relevant spoken tutorials on the spoken tutorial website. 9 00:00:40 --> 00:00:48 Perl provides a mechanism by which we can check a condition repeatedly for various values. This is done using loops. 10 00:00:49 --> 00:00:51 There are various types of loops in Perl; 11 00:00:52 --> 00:00:52 for loop 12 00:00:53 --> 00:00:53 foreach loop 13 00:00:54 --> 00:00:54 while loop & 14 00:00:55 --> 00:00:55 do-while loop 15 00:00:56 --> 00:01:00 In this tutorial, we'll cover for and foreach loop. 16 00:01:01 --> 00:01:06 for loop in Perl can be used to execute a piece of code for a certain number of times. 17 00:01:07 --> 00:01:09 The syntax of for loop is as follows: 18 00:01:10 --> 00:01:19 for space open bracket variable initialization semicolon condition semicolon increment 19 00:01:20 --> 00:01:21 close bracket Press Enter 20 00:01:22 --> 00:01:23 Open curly brackets 21 00:01:24 --> 00:01:27 Piece of code to be executed multiple times 22 00:01:28 --> 00:01:29 Close curly brackets 23 00:01:30 --> 00:01:32 Now let us look at an example of a for loop. 24 00:01:33 --> 00:01:41 Open the Terminal and type; gedit forLoop.pl space & (ampersand) 25 00:01:42 --> 00:01:42 and press Enter 26 00:01:43 --> 00:01:47 This will open the forLoop.pl file in gedit. 27 00:01:48 --> 00:01:57 Type the following piece of code;hash exclamation mark slash u s r slash bin slash perl 28 00:01:58 --> 00:01:59 Press Enter 29 00:02:00 --> 00:02:17 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 30 00:02:18 --> 00:02:18 space 31 00:02:19 --> 00:02:20 Open curly bracket press enter 32 00:02:21 --> 00:02:34 typeprint space double quote Value of i colon space dollar i slash n close double quote semicolon 33 00:02:35 --> 00:02:35 And Press Enter 34 00:02:36 --> 00:02:38 now close curly bracket 35 00:02:39 --> 00:02:41 Press Ctrl+S to save the file. 36 00:02:42 --> 00:02:45 Let me explain what the for loop does. 37 00:02:46 --> 00:02:49 The variable i is initialized to zero. 38 00:02:50 --> 00:02:52 Next, the condition is checked. 39 00:02:53 --> 00:02:58 In this case, the condition is i less than or equal to 4. 40 00:02:59 --> 00:03:04 If this condition is true, the code within the curly bracket will be executed. 41 00:03:05 --> 00:03:10 This means the first print statement "Value of i colon 0" 42 00:03:11 --> 00:03:13 will be displayed on the terminal. 43 00:03:14 --> 00:03:17 After this, the variable i is incremented by 1. 44 00:03:18 --> 00:03:22 And the for loop condition is checked once again. 45 00:03:23 --> 00:03:28 This loop will exit when the value of i becomes greater than 4. 46 00:03:29 --> 00:03:37 In this case, the for loop will be executed for i = 0, 1, 2, 3, 4 47 00:03:38 --> 00:03:40 which is a total of 5 times. 48 00:03:41 --> 00:03:43 Now, switch to terminal. 49 00:03:44 --> 00:03:47 Type the following to check for any compilation or syntax error: 50 00:03:48 --> 00:03:53 perl hyphen c forLoop dot pl 51 00:03:54 --> 00:03:55 and press Enter. 52 00:03:56 --> 00:03:57 Here it displays a message 53 00:03:58 --> 00:04:00 forLoop.pl syntax OK 54 00:04:01 --> 00:04:02 So, we have no errors. 55 00:04:03 --> 00:04:10 Now, let's execute the Perl script by typing perl forLoop dot pl and press Enter. 56 00:04:11 --> 00:04:15 The following output will be shown on terminal. 57 00:04:16 --> 00:04:18 Now, let us look at foreach loop. 58 00:04:19 --> 00:04:24 If we want to iterate a condition for an array, we can make use of foreach loop. 59 00:04:25 --> 00:04:34 The syntax is: foreach space dollar variable space within brackets at the rate array space 60 00:04:35 --> 00:04:36 open curly bracket 61 00:04:37 --> 00:04:41 perform action on each element of an array Press Enter 62 00:04:42 --> 00:04:43 Close the curly bracket. 63 00:04:44 --> 00:04:51 Please note: We'll cover array, array initialization and defining an array in subsequent tutorials. 64 00:04:52 --> 00:04:55 Now let us look at an example of foreach loop. 65 00:04:56 --> 00:05:07 Open the Terminal and type gedit foreachLoop dot pl space ampersandand Press Enter 66 00:05:08 --> 00:05:11 This will open the foreachLoop.pl file in gedit. 67 00:05:12 --> 00:05:14 Type the following piece of code 68 00:05:15 --> 00:05:24 hash exclamation mark slash u s r slash bin slash perl and Press Enter 69 00:05:25 --> 00:05:38 at the rate myarray space is equal to space open bracket ten comma twenty comma thirty close bracket semicolon 70 00:05:39 --> 00:05:40 press enter 71 00:05:41 --> 00:05:51 foreach space dollar var space open bracket at the rate myarray close bracket space 72 00:05:52 --> 00:05:55 Open curly bracket press enter and type 73 00:05:56 --> 00:06:12 'print space double quote Element of an array is colon dollar var slash n close double quote semicolon 74 00:06:13 --> 00:06:16 Press Enter and Close the curly bracket 75 00:06:17 --> 00:06:19 Press ctrl+s to save the file. 76 00:06:20 --> 00:06:26 Let me explain what this code does. An array myarray is declared. 77 00:06:27 --> 00:06:32 It has 3 elements 10, 20 and 30. 78 00:06:33 --> 00:06:39 In each iteration of foreach loop dollar var will contain the single element of an array 79 00:06:40 --> 00:06:46 foreach keyword will repeat this loop for each element of an array. 80 00:06:47 --> 00:06:54 That is, the code within the curly bracket will be executed for each myarray element. 81 00:06:55 --> 00:06:59 Back-slash n will place the prompt on a new line. 82 00:07:00 --> 00:07:05 This means the first element '10' will be displayed on the terminal. 83 00:07:06 --> 00:07:11 Then 20 and so on, till all the elements are printed. 84 00:07:12 --> 00:07:16 This loop will exit after printing all the elements in myarray. 85 00:07:17 --> 00:07:23 Now, switch to terminal and type the following to check for any compilation or syntax error. 86 00:07:24 --> 00:07:31 type perl hyphen c foreachLoop dot pland press Enter. 87 00:07:32 --> 00:07:35 The following line will be shown on terminal 88 00:07:36 --> 00:07:37 There are no compilation or syntax errors. 89 00:07:38 --> 00:07:40 So let us execute the Perl script. 90 00:07:41 --> 00:07:47 Type perl foreachLoop dot pl and press Enter 91 00:07:48 --> 00:07:53 The following output will be shown on terminal. 92 00:07:54 --> 00:07:56 So, that's it about for loop and foreach loop. 93 00:07:57 --> 00:07:58 Let us summarize. 94 00:07:59 --> 00:08:01 In this tutorial, we have learnt - 95 00:08:02 --> 00:08:05 for loop and foreach loop in Perl 96 00:08:06 --> 00:08:06 using some sample programs. 97 00:08:07 --> 00:08:09 Here is an assignment for you - 98 00:08:10 --> 00:08:12 Declare a string as 'Spoken Tutorial' and 99 00:08:13 --> 00:08:15 Print it 5 times 100 00:08:16 --> 00:08:31 Declare an array of colors as @colorArray = open bracket in single quote red comma white comma blue close the bracket and 101 00:08:32 --> 00:08:35 Print the element of an array using foreach loop 102 00:08:36 --> 00:08:39 Watch the video available at the following link 103 00:08:40 --> 00:08:42 It summaries the Spoken Tutorial project 104 00:08:43 --> 00:08:47 If you do not have good bandwidth, you can download and watch it 105 00:08:48 --> 00:08:54 The Spoken Tutorial Project Team Conducts workshops using spoken tutorials 106 00:08:55 --> 00:08:58 Gives certificates to those who pass an online test 107 00:08:59 --> 00:09:06 For more details, please write to contact at spoken hyphen tutorial dot org 108 00:09:07 --> 00:09:11 Spoken Tutorial Project is a part of the Talk to a Teacher project 109 00:09:12 --> 00:09:19 It is supported by the National Mission on Education through ICT, MHRD, Government of India. 110 00:09:20 --> 00:09:30 More information on this Mission is available spoken hyphen tutorial dot org slash NMEICT hyphen Intro 111 00:09:31 --> 00:09:33 Hope you enjoyed this Perl tutorial. 112 00:09:34 --> 00:09:35 This is Amol signing off. 113 00:09:36 --> 00:09:41 Thanks for joining.