Difference between revisions of "PERL/C2/for-for-each-loops/English-timed"
From Script | Spoken-Tutorial
Sandhya.np14 (Talk | contribs) |
|||
Line 5: | Line 5: | ||
|- | |- | ||
| 00:01 | | 00:01 | ||
− | |Welcome to the spoken tutorial on '''for '''and''' foreach Loops''' in '''Perl''' | + | |Welcome to the spoken tutorial on '''for '''and''' foreach Loops''' in '''Perl'''. |
|- | |- | ||
Line 12: | Line 12: | ||
|- | |- | ||
|00:11 | |00:11 | ||
− | |'''foreach''' loop in '''Perl''' | + | |'''foreach''' loop in '''Perl'''. |
|- | |- | ||
|00:13 | |00:13 | ||
− | | I am using '''Ubuntu | + | | I am using '''Ubuntu Linux 12.04''' operating system and '''Perl 5.14.2'''. |
|- | |- | ||
Line 28: | Line 28: | ||
|- | |- | ||
| 00:29 | | 00:29 | ||
− | | You should have basic knowledge of | + | | You should have basic knowledge of variables and comments in''' Perl.''' |
|- | |- | ||
Line 40: | Line 40: | ||
|- | |- | ||
| 00:49 | | 00:49 | ||
− | | There are various types of loops in Perl | + | | There are various types of loops in Perl: |
|- | |- | ||
|00:52 | |00:52 | ||
− | |'''for loop''' | + | |* '''for loop''' |
|- | |- | ||
|00:53 | |00:53 | ||
− | |'''foreach loop''' | + | |* '''foreach loop''' |
|- | |- | ||
|00:54 | |00:54 | ||
− | |'''while loop | + | |* '''while loop''' and |
|- | |- | ||
|00:55 | |00:55 | ||
− | |'''do-while loop''' | + | |* '''do-while loop'''. |
|- | |- | ||
|00:56 | |00:56 | ||
− | |In this tutorial, we | + | |In this tutorial, we will cover '''for''' and '''foreach loop'''. |
|- | |- | ||
|01:01 | |01:01 | ||
− | | '''for '''loop in '''Perl''' can be used to execute a piece of code for a certain number of times. | + | | '''for''' loop in '''Perl''' can be used to execute a piece of code for a certain number of times. |
|- | |- | ||
Line 74: | Line 74: | ||
|- | |- | ||
|01:20 | |01:20 | ||
− | |close bracket''' | + | |close bracket''', press '''Enter'''. |
|- | |- | ||
|01:22 | |01:22 | ||
− | |Open curly brackets | + | |Open curly brackets, |
|- | |- | ||
|01:24 | |01:24 | ||
− | | | + | |piece of code to be executed multiple times, |
|- | |- | ||
|01:28 | |01:28 | ||
− | | | + | |close curly brackets. |
|- | |- | ||
Line 94: | Line 94: | ||
|- | |- | ||
| 01:33 | | 01:33 | ||
− | | Open the '''Terminal''' and type | + | | Open the '''Terminal''' and type: '''gedit forLoop.pl space & (ampersand)''' |
|- | |- | ||
|01:42 | |01:42 | ||
− | |and press''' Enter''' | + | |and press''' Enter'''. |
|- | |- | ||
Line 106: | Line 106: | ||
|- | |- | ||
| 1:48 | | 1:48 | ||
− | | Type the following piece of code | + | | Type the following piece of code: '''hash exclamation mark slash u s r slash bin slash perl''' |
|- | |- | ||
|01:58 | |01:58 | ||
− | | | + | |press '''Enter'''. |
|- | |- | ||
Line 122: | Line 122: | ||
|- | |- | ||
|02:19 | |02:19 | ||
− | |Open curly bracket press | + | |Open curly bracket press '''Enter'''. |
|- | |- | ||
|02:21 | |02:21 | ||
− | | | + | | Type: '''print space double quote Value of i colon <space> dollar i slash n''' double quote complete semicolon |
|- | |- | ||
|02:35 | |02:35 | ||
− | | | + | | and press '''Enter'''. |
|- | |- | ||
|02:36 | |02:36 | ||
− | | | + | |Now close curly bracket. |
|- | |- | ||
Line 265: | Line 265: | ||
|- | |- | ||
|05:12 | |05:12 | ||
− | | Type the following piece of code | + | | Type the following piece of code: |
|- | |- | ||
|05:15 | |05:15 | ||
− | |'''hash exclamation mark slash u s r slash bin slash perl''' and | + | |'''hash exclamation mark slash u s r slash bin slash perl''' and press '''Enter'''. |
|- | |- | ||
Line 276: | Line 276: | ||
|- | |- | ||
|05:39 | |05:39 | ||
− | |press | + | |press '''Enter'''. |
|- | |- | ||
Line 339: | Line 339: | ||
|- | |- | ||
|07:24 | |07:24 | ||
− | | | + | |Type: '''perl hyphen c foreachLoop dot pl'''and press''' Enter.''' |
|- | |- | ||
| 07:32 | | 07:32 | ||
− | | The following line will be shown on terminal | + | | The following line will be shown on terminal. |
|- | |- | ||
Line 351: | Line 351: | ||
|- | |- | ||
|07:38 | |07:38 | ||
− | |So let us execute the '''Perl''' script. | + | |So, let us execute the '''Perl''' script. |
|- | |- | ||
|07:41 | |07:41 | ||
− | |Type '''perl foreachLoop dot pl''' and press '''Enter''' | + | |Type: '''perl foreachLoop dot pl''' and press '''Enter'''. |
|- | |- | ||
| 07:48 | | 07:48 | ||
− | | | + | | The following output will be shown on terminal. |
|- | |- | ||
| 07:54 | | 07:54 | ||
Line 366: | Line 366: | ||
|- | |- | ||
| 07:57 | | 07:57 | ||
− | | | + | | Let us summarize. |
|- | |- | ||
Line 382: | Line 382: | ||
|- | |- | ||
| 08:07 | | 08:07 | ||
− | | | + | |Here is an assignment for you - |
|- | |- | ||
Line 389: | Line 389: | ||
|- | |- | ||
|08:13 | |08:13 | ||
− | | | + | | print it 5 times. |
|- | |- | ||
|08:16 | |08:16 | ||
Line 395: | Line 395: | ||
|- | |- | ||
|08:32 | |08:32 | ||
− | | | + | | print the element of an array using '''foreach''' loop. |
|- | |- | ||
|08:36 | |08:36 | ||
− | |Watch the video available at the following link | + | |Watch the video available at the following link. |
|- | |- | ||
|08:40 | |08:40 | ||
− | |It summaries the Spoken Tutorial project | + | |It summaries the Spoken Tutorial project. |
|- | |- | ||
|08:43 | |08:43 | ||
− | |If you do not have good bandwidth, you can download and watch it | + | |If you do not have good bandwidth, you can download and watch it. |
|- | |- | ||
| 08:48 | | 08:48 | ||
− | | The Spoken Tutorial Project Team Conducts workshops using spoken tutorials | + | | The Spoken Tutorial Project Team: Conducts workshops using spoken tutorials. |
|- | |- | ||
|08:55 | |08:55 | ||
− | |Gives certificates to those who pass an online | + | |Gives certificates to those who pass an online test. |
|- | |- | ||
|08:59 | |08:59 | ||
− | |For more details, please write to contact at spoken hyphen tutorial dot org | + | |For more details, please write to contact at spoken hyphen tutorial dot org. |
|- | |- | ||
| 09:07 | | 09:07 | ||
− | | Spoken Tutorial | + | | "Spoken Tutorial" project is a part of the "Talk to a Teacher" project. |
|- | |- | ||
Line 431: | Line 431: | ||
|- | |- | ||
|09:20 | |09:20 | ||
− | |More information on this | + | |More information on this mission is available at spoken hyphen tutorial dot org slash NMEICT hyphen Intro. |
|- | |- | ||
| 09:31 | | 09:31 | ||
− | | | + | |Hope you enjoyed this '''Perl''' tutorial. |
|- | |- | ||
|09:34 | |09:34 | ||
− | |This is Amol signing off. | + | |This is Amol, signing off. |
|- | |- |
Revision as of 17:38, 7 May 2015
Time | Narration |
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 Linux 12.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 and |
00:55 | * do-while loop. |
00:56 | In this tutorial, we will 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 | Type: print space double quote Value of i colon <space> dollar i slash n double quote complete semicolon |
02:35 | and press Enter. |
02:36 | Now 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 less than or equal to 4. |
02:59 | If this condition is true, the code within the curly bracket will be executed. |
03:05 | This means the first print statement "Value of i colon 0" |
03:11 | will be displayed on the terminal. |
03:14 | After this, the variable i is incremented by 1. |
03:18 | 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 terminal. |
03:44 | Type the following to check for any compilation or syntax error: |
03:48 | 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:11 | The following output will be shown on terminal. |
04:16 | Now, let us look at 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 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:52 | Now let us look at an example of foreach loop. |
04:56 | 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:12 | 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 space is equal to space open bracket ten comma twenty comma 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 an array is colon dollar var slash n close double quote semicolon |
06:13 | Press Enter and Close the curly bracket |
06:17 | Press ctrl+s to save the file. |
06:20 | Let me explain what this code does. An array myarray is declared. |
06:27 | 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:40 | foreach keyword will repeat this loop for each element of an array. |
06:47 | That is, the code within the curly bracket will be executed for each myarray element. |
06:55 | Back-slash n will place the prompt on a new line. |
07:00 | This means the first element '10' will be displayed on the terminal. |
07:06 | 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:38 | So, let us execute the Perl script. |
07:41 | Type: perl foreachLoop dot pl and press Enter. |
07:48 | 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:02 | for loop and foreach loop in Perl |
08:06 | using some sample programs. |
08:07 | Here is an assignment for you - |
08:10 | Declare a string as 'Spoken Tutorial' and |
08:13 | print it 5 times. |
08:16 | Declare an array of colors as @colorArray = open bracket in single quote red comma white comma blue close the bracket 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:43 | If you do not have good bandwidth, you can download and watch it. |
08:48 | The Spoken Tutorial Project Team: Conducts workshops using spoken tutorials. |
08:55 | Gives certificates to those who pass an online test. |
08:59 | For more details, please write to contact at spoken hyphen tutorial dot org. |
09:07 | "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 at spoken hyphen tutorial dot org slash NMEICT hyphen Intro. |
09:31 | Hope you enjoyed this Perl tutorial. |
09:34 | This is Amol, signing off. |
09:36 | Thanks for joining. |