Difference between revisions of "PERL/C2/Arrays/English-timed"

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 1: Line 1:
 
{| border=1
 
{| border=1
!  <center>'''Time'''</center>
+
|'''Time'''
!  <center>'''Narration'''</center>
+
|'''Narration'''
 
   
 
   
 
|-
 
|-
| 00.01
+
| 00:01
 
|Welcome to the spoken tutorial on '''Arrays''' in '''Perl.'''
 
|Welcome to the spoken tutorial on '''Arrays''' in '''Perl.'''
  
 
|-
 
|-
| 00.06
+
| 00:06
 
| In this tutorial, we will learn about  
 
| In this tutorial, we will learn about  
  
 
|-
 
|-
|00.09
+
|00:09
 
|'''Index''' of an '''array'''  
 
|'''Index''' of an '''array'''  
 
   
 
   
 
|-
 
|-
|00.11
+
|00:11
 
| Length of an '''array '''
 
| Length of an '''array '''
 
   
 
   
 
|-
 
|-
|00.13
+
|00:13
 
|Accessing '''elements''' of an '''array'''  
 
|Accessing '''elements''' of an '''array'''  
 
   
 
   
 
|-
 
|-
| 00.16
+
| 00:16
 
| Looping over an '''array '''
 
| Looping over an '''array '''
  
 
|-
 
|-
| 00.18
+
| 00:18
 
|'''Sequential Array'''  
 
|'''Sequential Array'''  
  
 
|-
 
|-
| 00.20
+
| 00:20
 
| And '''Array Slicing '''
 
| And '''Array Slicing '''
  
 
|-
 
|-
|00.22
+
|00:22
 
|Here I am using ''' Ubuntu Linux12.04''' operating system and '''Perl 5.14.2'''
 
|Here I am using ''' Ubuntu Linux12.04''' operating system and '''Perl 5.14.2'''
 
   
 
   
 
|-
 
|-
|00.30
+
|00:30
 
|I will also be using the '''gedit''' Text Editor.  
 
|I will also be using the '''gedit''' Text Editor.  
 
   
 
   
 
|-
 
|-
| 00.34
+
| 00:34
 
| You can use any text editor of your choice.  
 
| You can use any text editor of your choice.  
 
   
 
   
 
|-
 
|-
|00.37
+
|00:37
 
|You should have basic knowledge of ''' Variables, Comments & Data Structures''' in '''Perl'''
 
|You should have basic knowledge of ''' Variables, Comments & Data Structures''' in '''Perl'''
  
 
|-
 
|-
|00.43
+
|00:43
 
|Knowledge of ''' loops and conditional statements''' will be an added advantage.
 
|Knowledge of ''' loops and conditional statements''' will be an added advantage.
 
   
 
   
 
|-
 
|-
|00.48
+
|00:48
 
|Please go through the relevant spoken tutorials on the '''spoken tutorial''' website.  
 
|Please go through the relevant spoken tutorials on the '''spoken tutorial''' website.  
  
 
|-
 
|-
|00.54
+
|00:54
 
|'''Array''' is a simple '''data structure''' which contains '''elements''' of any '''data type'''.   
 
|'''Array''' is a simple '''data structure''' which contains '''elements''' of any '''data type'''.   
  
 
|-
 
|-
|00.59
+
|00:59
 
|'''Array index''' starts from zero always.  
 
|'''Array index''' starts from zero always.  
  
 
|-
 
|-
| 01.03
+
| 01:03
 
| In '''Perl''', it is not necessary to declare the length of an '''array'''.   
 
| In '''Perl''', it is not necessary to declare the length of an '''array'''.   
  
 
|-
 
|-
|01.08
+
|01:08
 
|'''Array'''  length expands/shrinks as and when elements are added/removed from it.  
 
|'''Array'''  length expands/shrinks as and when elements are added/removed from it.  
  
 
|-
 
|-
|01.15
+
|01:15
 
| The syntax for declaring an array is -  
 
| The syntax for declaring an array is -  
 
   
 
   
 
|-
 
|-
|01.18
+
|01:18
 
|'''@myArray equal to open bracket 1 comma  2 comma 3 comma  single quote abc single quote  comma 10.3 close  bracket semicolon'''
 
|'''@myArray equal to open bracket 1 comma  2 comma 3 comma  single quote abc single quote  comma 10.3 close  bracket semicolon'''
  
 
|-
 
|-
|01.31
+
|01:31
 
|The last index of an array can be found with this command -  
 
|The last index of an array can be found with this command -  
 
   
 
   
 
|-
 
|-
|01.35
+
|01:35
 
|'''$#myArray '''
 
|'''$#myArray '''
 
   
 
   
 
|-
 
|-
|01.38
+
|01:38
 
| Let us understand this using sample program.  
 
| Let us understand this using sample program.  
 
   
 
   
 
|-
 
|-
| 01.42
+
| 01:42
 
|Open the terminal and type  
 
|Open the terminal and type  
  
 
|-
 
|-
|01.44
+
|01:44
 
|'''gedit arrayIndex dot pl space ampersand '''
 
|'''gedit arrayIndex dot pl space ampersand '''
 
   
 
   
 
|-
 
|-
|01.50
+
|01:50
 
| and press '''Enter. '''
 
| and press '''Enter. '''
  
 
|-
 
|-
| 01.52
+
| 01:52
 
| This will open '''arrayIndex dot pl''' file in '''geditor'''
 
| This will open '''arrayIndex dot pl''' file in '''geditor'''
 
   
 
   
 
|-
 
|-
|01.57
+
|01:57
 
|Type the piece of code that is displayed on screen   
 
|Type the piece of code that is displayed on screen   
 
    
 
    
 
|-
 
|-
|02.02
+
|02:02
 
|Here we have declared & defined an '''array''' which contains 5 elements.  
 
|Here we have declared & defined an '''array''' which contains 5 elements.  
 
   
 
   
 
|-
 
|-
|02.07
+
|02:07
 
|As array index starts from zero, the last index value will be 4  
 
|As array index starts from zero, the last index value will be 4  
 
   
 
   
 
|-
 
|-
|02.14
+
|02:14
 
|i.e number of elements, which is 5, minus 1.  
 
|i.e number of elements, which is 5, minus 1.  
 
   
 
   
 
|-
 
|-
|02.18
+
|02:18
 
|Press Ctrl+S to save the file.  
 
|Press Ctrl+S to save the file.  
  
 
|-
 
|-
|02.22
+
|02:22
 
|Now switch to  terminal and execute the Perl script
 
|Now switch to  terminal and execute the Perl script
  
 
|-
 
|-
|02.26
+
|02:26
 
| Type ''' perl arrayIndex dot pl '''
 
| Type ''' perl arrayIndex dot pl '''
  
 
|-
 
|-
|02.30
+
|02:30
 
|and press '''Enter.'''
 
|and press '''Enter.'''
 
   
 
   
 
|-
 
|-
|02.32
+
|02:32
 
|The output will be as displayed on the terminal  
 
|The output will be as displayed on the terminal  
 
   
 
   
 
|-
 
|-
|02.37
+
|02:37
 
|Now, let us see how to get length of an array in Perl.  
 
|Now, let us see how to get length of an array in Perl.  
 
   
 
   
 
|-
 
|-
|02.41
+
|02:41
 
|There are many ways by which we can find the length of an array.  
 
|There are many ways by which we can find the length of an array.  
  
 
|-
 
|-
|02.46
+
|02:46
 
|Index of an '''array + 1  i.e. $#array + 1 '''
 
|Index of an '''array + 1  i.e. $#array + 1 '''
 
   
 
   
 
|-
 
|-
|02.53
+
|02:53
 
|Using PERL inbuilt '''scalar function; i.e.  '''scalar''' open bracket '''@array''' close bracket  
 
|Using PERL inbuilt '''scalar function; i.e.  '''scalar''' open bracket '''@array''' close bracket  
 
   
 
   
 
|-
 
|-
|03.02
+
|03:02
 
|Assign array to a ''' scalar variable i.e. $arrayLength = @array '''
 
|Assign array to a ''' scalar variable i.e. $arrayLength = @array '''
 
   
 
   
 
|-
 
|-
|  03.09
+
|  03:09
 
| Let us look at an illustration of array length using a sample program.   
 
| Let us look at an illustration of array length using a sample program.   
 
   
 
   
 
|-
 
|-
|  03.14
+
|  03:14
 
| Switch to the terminal and type -   
 
| Switch to the terminal and type -   
  
 
|-
 
|-
| 03.18
+
| 03:18
 
| '''gedit arrayLength dot pl space ampersand '''
 
| '''gedit arrayLength dot pl space ampersand '''
 
   
 
   
 
|-
 
|-
|03.24
+
|03:24
 
|Press '''Enter.'''
 
|Press '''Enter.'''
 
   
 
   
 
|-
 
|-
| 03.27
+
| 03:27
 
|  Type the following piece of code, as shown on the screen-  
 
|  Type the following piece of code, as shown on the screen-  
 
   
 
   
 
|-
 
|-
| 03.32
+
| 03:32
 
|Here we have declared & defined an array which contains 5 elements.  
 
|Here we have declared & defined an array which contains 5 elements.  
 
   
 
   
 
|-
 
|-
|03.38
+
|03:38
 
|So, output will display 5.  
 
|So, output will display 5.  
 
   
 
   
 
|-
 
|-
|03.41
+
|03:41
 
|Highlighted, are various ways to find the length of an array in Perl.  
 
|Highlighted, are various ways to find the length of an array in Perl.  
 
   
 
   
 
|-
 
|-
|  03.47
+
|  03:47
 
| Please note We have '''concatenated''' the output in the print statement using comma.  
 
| Please note We have '''concatenated''' the output in the print statement using comma.  
 
   
 
   
 
|-
 
|-
|03.53
+
|03:53
 
|Press '''Ctrl + S''' to save the file.  
 
|Press '''Ctrl + S''' to save the file.  
 
    
 
    
 
|-
 
|-
|  03.57
+
|  03:57
 
| Now let us execute the script.  
 
| Now let us execute the script.  
 
   
 
   
 
|-
 
|-
|  03.59
+
|  03:59
 
|  Switch to terminal and type-  
 
|  Switch to terminal and type-  
  
 
|-
 
|-
|  04.02
+
|  04:02
 
| '''perl arrayLength dot pl''' and press '''Enter.'''
 
| '''perl arrayLength dot pl''' and press '''Enter.'''
 
   
 
   
 
|-
 
|-
| 04.07
+
| 04:07
 
| The output will be as displayed on the terminal.
 
| The output will be as displayed on the terminal.
  
 
|-
 
|-
| 04.12
+
| 04:12
 
| Now, let us understand how to access individual elements in an array.  
 
| Now, let us understand how to access individual elements in an array.  
  
 
|-
 
|-
|04.18
+
|04:18
 
|'''Indexing''' is used to access elements of an array.  
 
|'''Indexing''' is used to access elements of an array.  
 
   
 
   
 
|-
 
|-
|04.22
+
|04:22
 
|Let us look at an example for accessing elements of an array at -  
 
|Let us look at an example for accessing elements of an array at -  
 
   
 
   
 
|-
 
|-
|04.27
+
|04:27
 
|First Position  
 
|First Position  
  
 
|-
 
|-
|04.28
+
|04:28
 
|Last Position  
 
|Last Position  
  
 
|-
 
|-
|04.29
+
|04:29
 
|Any position  
 
|Any position  
 
   
 
   
 
|-
 
|-
| 04.32
+
| 04:32
 
| Switch to the terminal and type -  
 
| Switch to the terminal and type -  
  
 
|-
 
|-
| 04.35
+
| 04:35
 
| '''gedit perlArray dot pl space ampersand '''
 
| '''gedit perlArray dot pl space ampersand '''
 
   
 
   
 
|-
 
|-
|04.42
+
|04:42
 
|and press '''Enter.'''  
 
|and press '''Enter.'''  
 
   
 
   
 
|-
 
|-
|04.45
+
|04:45
 
|Type the following piece of code as shown.  
 
|Type the following piece of code as shown.  
 
   
 
   
 
|-
 
|-
|04.49
+
|04:49
 
|Please note-  
 
|Please note-  
 
   
 
   
 
|-
 
|-
|04.50
+
|04:50
 
|'''myArray''' is declared with '''@''' (at the rate) sign.  
 
|'''myArray''' is declared with '''@''' (at the rate) sign.  
 
   
 
   
 
|-
 
|-
|04.54
+
|04:54
 
|But, to access an array element we need to use ''' $ (dollar)''' sign.  
 
|But, to access an array element we need to use ''' $ (dollar)''' sign.  
  
 
|-
 
|-
|04.59
+
|04:59
 
|To access the element at any position, we need to pass '''index''' to an array  
 
|To access the element at any position, we need to pass '''index''' to an array  
  
 
|-
 
|-
|05.07
+
|05:07
 
|Here, to access the first element of '''myArray''' ,  
 
|Here, to access the first element of '''myArray''' ,  
  
 
|-
 
|-
|05.11
+
|05:11
 
|zero is passed as '''index. '''
 
|zero is passed as '''index. '''
  
 
|-
 
|-
|  05.16
+
|  05:16
 
|To access the last element of '''myArray''' , we have passed the last '''index''' of '''myArray.'''
 
|To access the last element of '''myArray''' , we have passed the last '''index''' of '''myArray.'''
 
   
 
   
 
|-
 
|-
|  05.24
+
|  05:24
 
| Recall, we had learnt about this earlier.  
 
| Recall, we had learnt about this earlier.  
 
   
 
   
 
|-
 
|-
|05.28
+
|05:28
 
| Press Ctrl + S to save the file.  
 
| Press Ctrl + S to save the file.  
  
 
|-
 
|-
|  05.30
+
|  05:30
 
| Switch to the terminal and execute the  script as -
 
| Switch to the terminal and execute the  script as -
  
 
|-
 
|-
|  05.36
+
|  05:36
 
| '''perl perlArray dot pl'''
 
| '''perl perlArray dot pl'''
 
   
 
   
 
|-
 
|-
| 05.41
+
| 05:41
 
| and press '''Enter.'''
 
| and press '''Enter.'''
  
 
|-
 
|-
|  05.43
+
|  05:43
 
| The output will be as shown on the terminal  
 
| The output will be as shown on the terminal  
  
 
|-
 
|-
|  05.47
+
|  05:47
 
| Now, let us understand, how to '''loop over'''  each element of an array.
 
| Now, let us understand, how to '''loop over'''  each element of an array.
  
 
|-
 
|-
|05.52
+
|05:52
 
|There are two ways of '''looping over''' an array-
 
|There are two ways of '''looping over''' an array-
 
   
 
   
 
|-
 
|-
|  05.56
+
|  05:56
 
|  Using '''for loop '''
 
|  Using '''for loop '''
 
    
 
    
 
|-
 
|-
|  05.58
+
|  05:58
 
| Using '''foreach loop '''
 
| Using '''foreach loop '''
  
 
|-
 
|-
|06.01
+
|06:01
 
|Let's learn how to use these loops to iterate over an array using a sample program.  
 
|Let's learn how to use these loops to iterate over an array using a sample program.  
  
 
|-
 
|-
| 06.07
+
| 06:07
 
|For this, switch to the terminal and type   
 
|For this, switch to the terminal and type   
  
 
|-
 
|-
|06.11
+
|06:11
 
| '''gedit loopingOverArray dot pl space ampersand '''
 
| '''gedit loopingOverArray dot pl space ampersand '''
  
 
|-
 
|-
|06.17
+
|06:17
 
|And Press '''Enter'''
 
|And Press '''Enter'''
 
   
 
   
 
|-
 
|-
| 06.20
+
| 06:20
 
| Type the piece of code as shown on the screen  
 
| Type the piece of code as shown on the screen  
  
 
|-
 
|-
| 06.24
+
| 06:24
 
| Here, we are printing each element of  array by iterating the index.  
 
| Here, we are printing each element of  array by iterating the index.  
  
 
|-
 
|-
|  06.31
+
|  06:31
 
| The '''for loop''' will execute till the value of i variable reaches the last '''index''' of an array.  
 
| The '''for loop''' will execute till the value of i variable reaches the last '''index''' of an array.  
 
   
 
   
 
|-
 
|-
|  06.38
+
|  06:38
 
|Here, '''foreach loop''' will be executed for each element of an array.
 
|Here, '''foreach loop''' will be executed for each element of an array.
 
   
 
   
 
|-
 
|-
|06.46
+
|06:46
 
| Once the array reaches its last element, it will exit the ''' foreach''' loop.  
 
| Once the array reaches its last element, it will exit the ''' foreach''' loop.  
 
   
 
   
 
|-
 
|-
|06.53
+
|06:53
 
|Please Note: If you are not aware of '''for''' and '''foreach''' loops,  
 
|Please Note: If you are not aware of '''for''' and '''foreach''' loops,  
 
   
 
   
 
|-
 
|-
|06.58
+
|06:58
 
|please go through the relevant spoken tutorials on  spoken tutorial website.  
 
|please go through the relevant spoken tutorials on  spoken tutorial website.  
 
 
 
 
 
|-
 
|-
|07.04
+
|07:04
 
| Now, Press Ctrl + S to save the file.  
 
| Now, Press Ctrl + S to save the file.  
  
 
|-
 
|-
|07.07
+
|07:07
 
|Then switch to the terminal and execute the script as -
 
|Then switch to the terminal and execute the script as -
  
 
|-
 
|-
|07.12
+
|07:12
 
| '''perl loopingOverArray dot pl'''
 
| '''perl loopingOverArray dot pl'''
  
 
|-
 
|-
|07.15
+
|07:15
 
| and press '''Enter.'''
 
| and press '''Enter.'''
 
   
 
   
 
|-
 
|-
|07.19
+
|07:19
 
|The output will be as displayed on the terminal.   
 
|The output will be as displayed on the terminal.   
  
 
|-
 
|-
|07.24
+
|07:24
 
| In '''Perl''', we can declare a '''sequential array''' as-  
 
| In '''Perl''', we can declare a '''sequential array''' as-  
  
 
|-
 
|-
|07.28
+
|07:28
 
|'''@alphaArray''' = open bracket '''a''' dot dot '''d''' close bracket semicolon  
 
|'''@alphaArray''' = open bracket '''a''' dot dot '''d''' close bracket semicolon  
  
 
|-
 
|-
|07.37
+
|07:37
 
|i.e '''alphaArray''' will contain elements 'a', 'b', 'c' and 'd'  
 
|i.e '''alphaArray''' will contain elements 'a', 'b', 'c' and 'd'  
  
 
|-
 
|-
|07.44
+
|07:44
 
|Similarly, '''@numericArray ''' equal to open bracket 1 dot dot 5 close bracket semicolon  is same as  
 
|Similarly, '''@numericArray ''' equal to open bracket 1 dot dot 5 close bracket semicolon  is same as  
 
'''@numericArray'''  equal to  open bracket 1 comma  2 comma 3 comma  4 comma 5  
 
'''@numericArray'''  equal to  open bracket 1 comma  2 comma 3 comma  4 comma 5  
  
 
|-
 
|-
|08.03
+
|08:03
 
|Perl also provides array slicing.  
 
|Perl also provides array slicing.  
  
 
|-
 
|-
|08.06
+
|08:06
 
|This is nothing but extracting part of an array and dumping it into a new array.  
 
|This is nothing but extracting part of an array and dumping it into a new array.  
  
 
|-
 
|-
|08.13
+
|08:13
 
|'''@array = 19  comma 23 comma 56 comma 45 comma 87 comma 89  close bracket semicolon '''
 
|'''@array = 19  comma 23 comma 56 comma 45 comma 87 comma 89  close bracket semicolon '''
  
 
|-
 
|-
|08.27
+
|08:27
 
| '''@newArray''' = '''@array ''' open square bracket 1 comma 4 close square bracket semicolon  
 
| '''@newArray''' = '''@array ''' open square bracket 1 comma 4 close square bracket semicolon  
  
 
|-
 
|-
|08.38
+
|08:38
 
|After slicing, the  newArray will look like  
 
|After slicing, the  newArray will look like  
  
 
|-
 
|-
|08.42
+
|08:42
 
|'''@newArray''' =  open bracket 23 comma 87 close bracket semicolon  
 
|'''@newArray''' =  open bracket 23 comma 87 close bracket semicolon  
  
 
|-
 
|-
|08.51
+
|08:51
 
|Let us summarize.  
 
|Let us summarize.  
  
 
|-
 
|-
|08.52
+
|08:52
 
|In this tutorial, we have learnt to-  
 
|In this tutorial, we have learnt to-  
  
 
|-
 
|-
|08.55
+
|08:55
 
|Find index of an array  
 
|Find index of an array  
  
 
|-
 
|-
|08.57
+
|08:57
 
|Find length of an array  
 
|Find length of an array  
  
 
|-
 
|-
|08.59
+
|08:59
 
|Access elements of an array  
 
|Access elements of an array  
  
 
|-
 
|-
|09.01
+
|09:01
 
|Loop over an array  
 
|Loop over an array  
  
 
|-
 
|-
|09.03
+
|09:03
 
|Sequenial Array  
 
|Sequenial Array  
  
 
|-
 
|-
|09.05
+
|09:05
 
|Array Slicing using sample programs.  
 
|Array Slicing using sample programs.  
  
 
|-
 
|-
|09.07
+
|09:07
 
|Here is assignment for you -  
 
|Here is assignment for you -  
  
 
|-
 
|-
|09.10
+
|09:10
 
|Declare an array of rainbow colors  
 
|Declare an array of rainbow colors  
  
 
|-
 
|-
|09.13
+
|09:13
 
|Print 4th element of this array  
 
|Print 4th element of this array  
  
 
|-
 
|-
|09.16
+
|09:16
 
|Print Length and last index of this array  
 
|Print Length and last index of this array  
  
 
|-
 
|-
|09.19
+
|09:19
 
|Loop over each element of an array using for & foreach loops  
 
|Loop over each element of an array using for & foreach loops  
  
 
|-
 
|-
|09.25
+
|09:25
 
|Declare array as @myArray = open bracket 1..9 close bracket semicolon and then create an array of odd numbers from above array using array slicing.  
 
|Declare array as @myArray = open bracket 1..9 close bracket semicolon and then create an array of odd numbers from above array using array slicing.  
  
 
|-
 
|-
|09.41
+
|09:41
 
|Watch the video available at the following link
 
|Watch the video available at the following link
 
 
   
 
   
 
|-
 
|-
|09.44
+
|09:44
 
|It summaries the Spoken Tutorial project
 
|It summaries the Spoken Tutorial project
 
    
 
    
 
|-
 
|-
|09.48
+
|09:48
 
|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
 
   
 
   
 
|-
 
|-
| 09.53
+
| 09:53
 
|  The Spoken Tutorial Project Team  Conducts workshops using spoken tutorials
 
|  The Spoken Tutorial Project Team  Conducts workshops using spoken tutorials
 
   
 
   
 
|-
 
|-
|09.58
+
|09:58
 
|Gives certificates to those who pass an online  test
 
|Gives certificates to those who pass an online  test
 
   
 
   
 
|-
 
|-
|10.02
+
|10:02
 
|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
 
   
 
   
 
|-
 
|-
| 10.09
+
| 10:09
 
|  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
 
    
 
    
 
|-
 
|-
|10.13
+
|10:13
 
|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.
 
   
 
   
 
|-
 
|-
|10.20
+
|10:20
 
|More information on this Mission is available at 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
 
   
 
   
 
|-
 
|-
| 10.31
+
| 10:31
 
|  Hope you enjoyed this Perl tutorial.  
 
|  Hope you enjoyed this Perl tutorial.  
 
   
 
   
 
|-
 
|-
|10.35
+
|10:35
 
|This is Amol signing off.
 
|This is Amol signing off.
 
    
 
    
 
|-
 
|-
|10.37
+
|10:37
 
|Thanks for joining.
 
|Thanks for joining.
 
   
 
   
 
|}
 
|}

Revision as of 11:16, 10 July 2014

Time Narration
00:01 Welcome to the spoken tutorial on Arrays in Perl.
00:06 In this tutorial, we will learn about
00:09 Index of an array
00:11 Length of an array
00:13 Accessing elements of an array
00:16 Looping over an array
00:18 Sequential Array
00:20 And Array Slicing
00:22 Here I am using Ubuntu Linux12.04 operating system and Perl 5.14.2
00:30 I will also be using the gedit Text Editor.
00:34 You can use any text editor of your choice.
00:37 You should have basic knowledge of Variables, Comments & Data Structures in Perl
00:43 Knowledge of loops and conditional statements will be an added advantage.
00:48 Please go through the relevant spoken tutorials on the spoken tutorial website.
00:54 Array is a simple data structure which contains elements of any data type.
00:59 Array index starts from zero always.
01:03 In Perl, it is not necessary to declare the length of an array.
01:08 Array length expands/shrinks as and when elements are added/removed from it.
01:15 The syntax for declaring an array is -
01:18 @myArray equal to open bracket 1 comma 2 comma 3 comma single quote abc single quote comma 10.3 close bracket semicolon
01:31 The last index of an array can be found with this command -
01:35 $#myArray
01:38 Let us understand this using sample program.
01:42 Open the terminal and type
01:44 gedit arrayIndex dot pl space ampersand
01:50 and press Enter.
01:52 This will open arrayIndex dot pl file in geditor
01:57 Type the piece of code that is displayed on screen
02:02 Here we have declared & defined an array which contains 5 elements.
02:07 As array index starts from zero, the last index value will be 4
02:14 i.e number of elements, which is 5, minus 1.
02:18 Press Ctrl+S to save the file.
02:22 Now switch to terminal and execute the Perl script
02:26 Type perl arrayIndex dot pl
02:30 and press Enter.
02:32 The output will be as displayed on the terminal
02:37 Now, let us see how to get length of an array in Perl.
02:41 There are many ways by which we can find the length of an array.
02:46 Index of an array + 1 i.e. $#array + 1
02:53 Using PERL inbuilt scalar function; i.e. scalar open bracket @array close bracket
03:02 Assign array to a scalar variable i.e. $arrayLength = @array
03:09 Let us look at an illustration of array length using a sample program.
03:14 Switch to the terminal and type -
03:18 gedit arrayLength dot pl space ampersand
03:24 Press Enter.
03:27 Type the following piece of code, as shown on the screen-
03:32 Here we have declared & defined an array which contains 5 elements.
03:38 So, output will display 5.
03:41 Highlighted, are various ways to find the length of an array in Perl.
03:47 Please note We have concatenated the output in the print statement using comma.
03:53 Press Ctrl + S to save the file.
03:57 Now let us execute the script.
03:59 Switch to terminal and type-
04:02 perl arrayLength dot pl and press Enter.
04:07 The output will be as displayed on the terminal.
04:12 Now, let us understand how to access individual elements in an array.
04:18 Indexing is used to access elements of an array.
04:22 Let us look at an example for accessing elements of an array at -
04:27 First Position
04:28 Last Position
04:29 Any position
04:32 Switch to the terminal and type -
04:35 gedit perlArray dot pl space ampersand
04:42 and press Enter.
04:45 Type the following piece of code as shown.
04:49 Please note-
04:50 myArray is declared with @ (at the rate) sign.
04:54 But, to access an array element we need to use $ (dollar) sign.
04:59 To access the element at any position, we need to pass index to an array
05:07 Here, to access the first element of myArray ,
05:11 zero is passed as index.
05:16 To access the last element of myArray , we have passed the last index of myArray.
05:24 Recall, we had learnt about this earlier.
05:28 Press Ctrl + S to save the file.
05:30 Switch to the terminal and execute the script as -
05:36 perl perlArray dot pl
05:41 and press Enter.
05:43 The output will be as shown on the terminal
05:47 Now, let us understand, how to loop over each element of an array.
05:52 There are two ways of looping over an array-
05:56 Using for loop
05:58 Using foreach loop
06:01 Let's learn how to use these loops to iterate over an array using a sample program.
06:07 For this, switch to the terminal and type
06:11 gedit loopingOverArray dot pl space ampersand
06:17 And Press Enter
06:20 Type the piece of code as shown on the screen
06:24 Here, we are printing each element of array by iterating the index.
06:31 The for loop will execute till the value of i variable reaches the last index of an array.
06:38 Here, foreach loop will be executed for each element of an array.
06:46 Once the array reaches its last element, it will exit the foreach loop.
06:53 Please Note: If you are not aware of for and foreach loops,
06:58 please go through the relevant spoken tutorials on spoken tutorial website.
07:04 Now, Press Ctrl + S to save the file.
07:07 Then switch to the terminal and execute the script as -
07:12 perl loopingOverArray dot pl
07:15 and press Enter.
07:19 The output will be as displayed on the terminal.
07:24 In Perl, we can declare a sequential array as-
07:28 @alphaArray = open bracket a dot dot d close bracket semicolon
07:37 i.e alphaArray will contain elements 'a', 'b', 'c' and 'd'
07:44 Similarly, @numericArray equal to open bracket 1 dot dot 5 close bracket semicolon is same as

@numericArray equal to open bracket 1 comma 2 comma 3 comma 4 comma 5

08:03 Perl also provides array slicing.
08:06 This is nothing but extracting part of an array and dumping it into a new array.
08:13 @array = 19 comma 23 comma 56 comma 45 comma 87 comma 89 close bracket semicolon
08:27 @newArray = @array open square bracket 1 comma 4 close square bracket semicolon
08:38 After slicing, the newArray will look like
08:42 @newArray = open bracket 23 comma 87 close bracket semicolon
08:51 Let us summarize.
08:52 In this tutorial, we have learnt to-
08:55 Find index of an array
08:57 Find length of an array
08:59 Access elements of an array
09:01 Loop over an array
09:03 Sequenial Array
09:05 Array Slicing using sample programs.
09:07 Here is assignment for you -
09:10 Declare an array of rainbow colors
09:13 Print 4th element of this array
09:16 Print Length and last index of this array
09:19 Loop over each element of an array using for & foreach loops
09:25 Declare array as @myArray = open bracket 1..9 close bracket semicolon and then create an array of odd numbers from above array using array slicing.
09:41 Watch the video available at the following link
09:44 It summaries the Spoken Tutorial project
09:48 If you do not have good bandwidth, you can download and watch it
09:53 The Spoken Tutorial Project Team Conducts workshops using spoken tutorials
09:58 Gives certificates to those who pass an online test
10:02 For more details, please write to contact at spoken hyphen tutorial dot org
10:09 Spoken Tutorial Project is a part of the Talk to a Teacher project
10:13 It is supported by the National Mission on Education through ICT, MHRD, Government of India.
10:20 More information on this Mission is available at spoken hyphen tutorial dot org slash NMEICT hyphen Intro
10:31 Hope you enjoyed this Perl tutorial.
10:35 This is Amol signing off.
10:37 Thanks for joining.

Contributors and Content Editors

Gaurav, PoojaMoolya, Sandhya.np14