Difference between revisions of "PERL/C2/for-for-each-loops/English-timed"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(8 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
{| border=1
 
{| border=1
! <center>'''Visual Cue'''</center>
+
| '''Time'''
!  <center>'''Narration'''</center>
+
| '''Narration'''
 
   
 
   
 
|-
 
|-
| 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'''.
 
   
 
   
 
|-
 
|-
| 00.06
+
| 00:06
 
| In this tutorial, we will learn about: '''for''' loop in '''Perl '''and
 
| In this tutorial, we will learn about: '''for''' loop in '''Perl '''and
 
|-
 
|-
|00.11
+
|00:11
|'''foreach''' loop in '''Perl'''
+
|'''foreach''' loop in '''Perl'''.
 
   
 
   
 
|-
 
|-
|00.13
+
|00:13
| I am using '''Ubuntu Linux12.04''' operating system and '''Perl 5.14.2'''
+
| I am using '''Ubuntu Linux 12.04''' operating system and '''Perl 5.14.2'''.
 
   
 
   
 
|-
 
|-
|00.21
+
|00:21
 
|I will also be using the '''gedit '''Text Editor.
 
|I will also be using the '''gedit '''Text Editor.
 
   
 
   
 
|-
 
|-
|00.25
+
|00:25
 
|You can use any text editor of your choice.
 
|You can use any text editor of your choice.
 
   
 
   
 
|-
 
|-
| 00.29
+
| 00:29
| You should have basic knowledge of '''Variables '''and''' Comments '''in''' Perl.'''
+
| You should have basic knowledge of variables and comments in''' Perl.'''
 
   
 
   
 
|-
 
|-
|00.33
+
|00:33
 
|If not, please go through the relevant spoken tutorials on the '''spoken tutorial '''website.
 
|If not, please go through the relevant spoken tutorials on the '''spoken tutorial '''website.
 
   
 
   
 
|-
 
|-
|00.40
+
|00:40
 
|  '''Perl''' provides a mechanism by which we can check a condition repeatedly for various values. This is done using '''loops'''.
 
|  '''Perl''' provides a mechanism by which we can check a condition repeatedly for various values. This is done using '''loops'''.
 
   
 
   
 
|-
 
|-
| 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''', '''foreach loop'''
 
   
 
   
 
|-
 
|-
|00.53
+
|00:54
|'''foreach loop'''
+
|'''while loop''' and '''do-while loop'''.
+
|-
+
|00.54
+
|'''while loop &'''
+
+
|-
+
|00.55
+
|'''do-while loop'''
+
  
 
|-
 
|-
|00.56
+
|00:56
|In this tutorial, we'll cover '''for''' and '''foreach loop'''.
+
|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.
 
   
 
   
 
|-
 
|-
| 01.07
+
| 01:07
 
| The syntax of '''for loop''' is as follows:
 
| The syntax of '''for loop''' is as follows:
 
|-
 
|-
|01.10
+
|01:10
 
|'''for space open bracket variable initialization semicolon condition semicolon increment
 
|'''for space open bracket variable initialization semicolon condition semicolon increment
 
|-
 
|-
|01.20
+
|01:20
|close bracket''' Press '''Enter'''
+
|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
+
|piece of code to be executed multiple times,
  
 
|-
 
|-
|01.28
+
|01:28
|Close curly brackets
+
|close curly brackets.
 
   
 
   
 
|-
 
|-
|01.30
+
|01:30
 
| Now let us look at an example of a '''for '''loop.
 
| Now let us look at an example of a '''for '''loop.
 
   
 
   
 
|-
 
|-
| 01.33
+
| 01:33
| Open the '''Terminal''' and type; '''gedit forLoop.pl space & (ampersand)'''
+
| Open the '''Terminal''' and type: '''gedit forLoop.pl space & (ampersand)'''
 
   
 
   
 
|-
 
|-
|01.42
+
|01:42
|and press''' Enter'''
+
|and press''' Enter'''. This will open the '''forLoop.pl''' file in '''gedit'''.
 
   
 
   
 
|-
 
|-
|01.43
+
|01:48
| This will open the '''forLoop.pl''' file in '''gedit'''.
+
| Type the following piece of code: '''hash exclamation mark slash u s r slash bin slash perl'''
 
   
 
   
 
|-
 
|-
| 1.48
+
|01:58
| Type the following piece of code;'''hash exclamation mark slash u s r slash bin slash perl'''
+
|press '''Enter'''.
 
   
 
   
 
|-
 
|-
|01.58
+
|02:00     
|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'''
 
|'''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
+
|02:18
|space
+
|space Open curly bracket  press '''Enter'''.
 
   
 
   
 
|-
 
|-
|02.19
+
|02:21
|Open curly bracket press enter
+
| Type: '''print space double quote Value of i colon <space> dollar i backslash n''' double quote complete semicolon
 
   
 
   
 
|-
 
|-
|02.21
+
|02:35
| type'''print space double quote Value of i colon space dollar i slash n''' close double quote '''semicolon '''
+
| and press '''Enter'''. Now close curly bracket.
 
   
 
   
 
|-
 
|-
|02.35
+
|02:39
| And Press '''Enter'''
+
+
|-
+
|02.36
+
|now close curly bracket
+
+
|-
+
|02.39
+
 
|Press '''Ctrl+S''' to '''save''' the file.
 
|Press '''Ctrl+S''' to '''save''' the file.
 
   
 
   
 
|-
 
|-
| 02.42     
+
| 02:42     
 
| Let me explain what the '''for''' loop does.
 
| Let me explain what the '''for''' loop does.
 +
 
|-
 
|-
|02.46
+
|02:46
 
|The variable '''i''' is initialized to zero.
 
|The variable '''i''' is initialized to zero.
 
   
 
   
 
|-
 
|-
|02.50
+
|02:50
 
|  Next, the condition is checked.  
 
|  Next, the condition is checked.  
 
   
 
   
 
|-
 
|-
|02.53
+
|02:53
 
|In this case, the condition is '''i less than or equal to 4'''.
 
|In this case, the condition is '''i less than or equal to 4'''.
 
   
 
   
 
|-
 
|-
|02.59
+
|02:59
 
|If this condition is true, the code within the curly bracket will be executed.
 
|If this condition is true, the code within the curly bracket will be executed.
 
   
 
   
 
|-
 
|-
| 03.05
+
| 03:05
| This means the first '''print''' statement  '''"Value of i colon 0"'''
+
|This means the first print statement  "Value of i colon 0"
 
   
 
   
 
|-
 
|-
|03.11
+
|03:11
|will be displayed on the '''terminal'''.
+
|will be displayed on the terminal.
 
   
 
   
 
|-
 
|-
| 03.14
+
|03:14
| After this, the variable '''i''' is incremented by '''1.'''
+
| After this, the variable 'i' is incremented by 1.  
 
   
 
   
 
|-
 
|-
|   03.18
+
| 03:18
 
| And the '''for''' loop condition is checked once again.
 
| And the '''for''' loop condition is checked once again.
 
   
 
   
 
|-
 
|-
|03.23
+
|03:23
|This loop will exit when the value of''' i''' becomes greater than '''4'''.
+
|This loop will exit when the value of ' i' becomes greater than 4.
 
   
 
   
 
|-
 
|-
| 03.29
+
|03:29
| In this case, the '''for''' loop will be executed for '''i = 0, 1, 2, 3, 4'''
+
|In this case, the '''for''' loop will be executed for i = 0, 1, 2, 3, and 4.
  
 
|-
 
|-
|03.38
+
|03:38
|which is a total of '''5 '''times.
+
|which is a total of 5 times.
 
   
 
   
 
|-
 
|-
| 03.41
+
|03:41
| Now, switch to '''terminal'''.
+
|Now, switch to terminal.
 
   
 
   
 
|-
 
|-
| 03.44
+
|03:44
| Type the following to check for any compilation or syntax error:
+
|Type the following to check for any compilation or syntax error:
 
   
 
   
 
|-
 
|-
|03.48
+
|03:48
 
|'''perl hyphen c forLoop dot pl'''
 
|'''perl hyphen c forLoop dot pl'''
 
   
 
   
 
|-
 
|-
|03.54  
+
|03:54  
 
|and press''' Enter.'''
 
|and press''' Enter.'''
 
   
 
   
 
|-
 
|-
| 03.56
+
|03:56
| Here it displays a message  
+
|Here it displays a message:
 
   
 
   
 
|-
 
|-
|03.58
+
|03:58
|'''forLoop.pl syntax OK'''
+
|'''forLoop.pl syntax OK'''.
 
   
 
   
 
|-
 
|-
|04.01
+
|04:01
 
|So, we have no errors.
 
|So, we have no errors.
 
   
 
   
 
|-
 
|-
| 04.03
+
|04:03
| Now, let's execute the '''Perl''' script by typing '''perl forLoop dot pl''' and press '''Enter.'''
+
| Now, let us execute the '''Perl''' script by typing '''perl forLoop dot pl''' and press '''Enter.'''
 
   
 
   
 
|-
 
|-
|   04.11
+
|04:11
| The following output will be shown on terminal.
+
|The following output will be shown on the terminal.
 
   
 
   
 
|-
 
|-
| 04.16
+
|04:16
 
| Now, let us look at  '''foreach '''loop.
 
| Now, let us look at  '''foreach '''loop.
 
   
 
   
 
|-
 
|-
| 04.19
+
| 04:19
 
| If we want to iterate a condition for an '''array''', we can make use of '''foreach '''loop.
 
| If we want to iterate a condition for an '''array''', we can make use of '''foreach '''loop.
 
   
 
   
 
|-
 
|-
| 04.25
+
| 04:25
 
|  The syntax is: '''foreach space dollar variable space within brackets at the rate array ''' space  
 
|  The syntax is: '''foreach space dollar variable space within brackets at the rate array ''' space  
  
 
|-
 
|-
|04.35
+
|04:35
 
|open curly bracket  
 
|open curly bracket  
 
   
 
   
 
|-
 
|-
|04.37
+
|04:37
|'''perform action on each element of an array ''' Press '''Enter'''
+
|'''perform action on each element of an array ''' press '''Enter''',
 
   
 
   
 
|-
 
|-
|04.42
+
|04:42
|Close the curly bracket.
+
|close the curly bracket.
 
   
 
   
 
|-
 
|-
|04.44
+
|04:44
|'''Please note: '''We'll cover array, array initialization and defining an array in subsequent tutorials.
+
|Please note: We'll cover array, array initialization and defining an array in subsequent tutorials.
 
   
 
   
 
|-
 
|-
|04.52
+
|04:52
 
|  Now let us look at an example of '''foreach''' loop.
 
|  Now let us look at an example of '''foreach''' loop.
 
   
 
   
 
|-
 
|-
| 04.56
+
| 04:56
|  Open the '''Terminal''' and type '''gedit foreachLoop dot pl space ampersand'''and Press''' Enter'''
+
|  Open the Terminal and type: '''gedit foreachLoop dot pl space ampersand''' and press''' Enter'''.
 
   
 
   
 
|-
 
|-
| 05.08
+
| 05:08
 
|  This will open the '''foreachLoop.pl''' file in '''gedit'''.
 
|  This will open the '''foreachLoop.pl''' file in '''gedit'''.
 
   
 
   
 
|-
 
|-
|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 Press '''Enter'''
+
|'''hash exclamation mark slash u s r slash bin slash perl''' and press '''Enter'''.
 
   
 
   
 
|-
 
|-
|05.25
+
|05:25
|'''at the rate myarray space is equal to space open bracket ten comma twenty comma thirty close bracket semicolon'''
+
|'''at the rate myarray space equal to space open bracket ten comma twenty comma thirty close the bracket semicolon'''
 
   
 
   
 
|-
 
|-
|05.39
+
|05:39
|press enter
+
|press '''Enter'''.
 
   
 
   
 
|-
 
|-
|05.41
+
|05:41
|'''foreach space dollar var space open bracket at the rate myarray close bracket space'''
+
|'''foreach space dollar var space open bracket at the rate myarray close the bracket space'''
 
|-
 
|-
|05.52
+
|05:52
|Open curly bracket press enter and type
+
|open curly bracket press '''Enter''' and type:
 
   
 
   
 
|-
 
|-
|05.56
+
|05:56
|'''print space double quote Element of an array is colon dollar var slash n'' close double quote  '''semicolon'''
+
|'''print space double quotes Element of an array is colon dollar var backslash n''double quotes complete semicolon  
  
 
|-
 
|-
|06.13
+
|06:13
|Press '''Enter''' and Close the curly bracket
+
|press '''Enter''' and close the curly bracket.
 
   
 
   
 
|-
 
|-
| 06.17
+
|06:17
| Press '''ctrl+s''' to '''save''' the file.
+
|Press '''Ctrl+s''' to '''save''' the file.
 
   
 
   
 
|-
 
|-
|  06.20
+
|  06:20
 
| Let me explain what this code does. An array '''myarray '''is declared.  
 
| Let me explain what this code does. An array '''myarray '''is declared.  
 
   
 
   
 
|-
 
|-
|06.27
+
|06:27
 
|It has 3 elements 10, 20 and 30.
 
|It has 3 elements 10, 20 and 30.
 
   
 
   
 
|-
 
|-
|  06 .33
+
|  06:33
|  In each iteration of foreach loop dollar var will contain the single element of an array
+
|  In each iteration of '''foreach''' loop, 'dollar var' ($var) will contain the single element of an array.
 
   
 
   
 
|-
 
|-
|  06.40
+
|  06:40
 
|  '''foreach '''keyword will repeat this loop for each element of an array.
 
|  '''foreach '''keyword will repeat this loop for each element of an array.
 
   
 
   
 
|-
 
|-
| 06.47
+
| 06:47
 
|  That is, the code within the curly bracket will be executed for each '''myarray '''element.
 
|  That is, the code within the curly bracket will be executed for each '''myarray '''element.
 
   
 
   
 
|-
 
|-
|  06.55
+
|  06:55
| '''Back-slash n''' will place the prompt on a new line.
+
| '''Back-slash n''' (\n) will place the prompt on a new line.
 
   
 
   
 
|-
 
|-
|  07.00
+
|  07:00
| This means the first '''element''' ''''10'''' will be displayed on the terminal.
+
| This means, the first '''element''' 10 will be displayed on the terminal.
  
 
|-
 
|-
|07.06
+
|07:06
 
|Then 20 and so on, till all the elements are printed.
 
|Then 20 and so on, till all the elements are printed.
 
   
 
   
 
|-
 
|-
|  07.12
+
|  07:12
 
|  This loop will exit after printing all the elements in '''myarray'''.
 
|  This loop will exit after printing all the elements in '''myarray'''.
 
   
 
   
 
|-
 
|-
|  07.17
+
|  07:17
 
| Now, switch to terminal and type the following to check for any compilation or syntax error.
 
| Now, switch to terminal and type the following to check for any compilation or syntax error.
 
   
 
   
 
|-
 
|-
|07.24
+
|07:24
|type '''perl hyphen c foreachLoop dot pl'''and press''' Enter.'''
+
|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 the terminal.
 
   
 
   
 
|-
 
|-
| 07.36
+
| 07:36
 
|  There are no compilation or syntax errors.
 
|  There are no compilation or syntax errors.
 
   
 
   
 
|-
 
|-
|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.
+
| The following output will be shown on the terminal.
 
|-
 
|-
|  07.54
+
|  07:54
 
| So, that's it about '''for''' loop and '''foreach''' loop.
 
| So, that's it about '''for''' loop and '''foreach''' loop.
 
   
 
   
 
|-
 
|-
|  07.57
+
|  07:57
| Let us summarize.
+
| Let us summarize.
 
   
 
   
 
|-
 
|-
|07.59
+
|07:59
 
|In this tutorial, we have learnt -
 
|In this tutorial, we have learnt -
 
   
 
   
 
|-
 
|-
|08.02
+
|08:02
 
|'''for''' loop and '''foreach''' loop in '''Perl'''
 
|'''for''' loop and '''foreach''' loop in '''Perl'''
 
   
 
   
 
|-
 
|-
|08.06
+
|08:06
|using some sample programs.
+
|using some sample programs. Here is an assignment for you -
+
|-
+
| 08.07
+
Here is an assignment for you -
+
 
   
 
   
 
|-
 
|-
|08.10
+
|08:10
| Declare a string as 'Spoken Tutorial' and  
+
| Declare a '''string''' as 'Spoken Tutorial' and  
 
|-
 
|-
|08.13
+
|08:13
| Print it 5 times
+
| print it 5 times.
 
|-
 
|-
|08.16
+
|08:16
 
| Declare an array of colors as @colorArray = open bracket in single quote red comma white comma  blue close the bracket and  
 
| Declare an array of colors as @colorArray = open bracket in single quote red comma white comma  blue close the bracket and  
 
|-
 
|-
|08.32
+
|08:32
| Print the element of an array using '''foreach''' loop
+
| 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 summarizes 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 test
+
|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 Project is a part of the Talk to a Teacher project
+
"Spoken Tutorial" project is a part of the "Talk to a Teacher" project.
 
   
 
   
 
|-
 
|-
|09.12
+
|09:12
 
|It is supported by the National Mission on Education through ICT, MHRD, Government of India.
 
|It is supported by the National Mission on Education through ICT, MHRD, Government of India.
 
   
 
   
 
|-
 
|-
|09.20
+
|09:20
|More information on this Mission is available spoken hyphen tutorial dot org slash NMEICT hyphen Intro
+
|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.  
+
|Hope you enjoyed this '''Perl''' tutorial.  
 
   
 
   
 
|-
 
|-
|09.34
+
|09:34
|This is Amol signing off.
+
|This is Amol, signing off.
  
 
|-
 
|-
|09.36
+
|09:36
 
|Thanks for joining.
 
|Thanks for joining.
 
   
 
   
 
|}
 
|}

Latest revision as of 15:08, 10 March 2017

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, foreach loop
00:54 while loop and 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. This will open the forLoop.pl file in gedit.
01: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 Open curly bracket press Enter.
02:21 Type: print space double quote Value of i colon <space> dollar i backslash n double quote complete semicolon
02:35 and press Enter. 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, and 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 us execute the Perl script by typing perl forLoop dot pl and press Enter.
04:11 The following output will be shown on the 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 ampersand and 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 equal to space open bracket ten comma twenty comma thirty close the bracket semicolon
05:39 press Enter.
05:41 foreach space dollar var space open bracket at the rate myarray close the bracket space
05:52 open curly bracket press Enter and type:
05:56 print space double quotes Element of an array is colon dollar var backslash n double quotes complete 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' ($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 (\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 pl and press Enter.
07:32 The following line will be shown on the 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 the 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. 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 summarizes 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.

Contributors and Content Editors

Gaurav, Madhurig, PoojaMoolya, Sandhya.np14, Sneha