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

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 13: Line 13:
 
|-
 
|-
 
|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  
 
   
 
   
 
|-
 
|-
Line 53: Line 53:
 
|-
 
|-
 
|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.
 
   
 
   
 
|-
 
|-
Line 73: Line 73:
 
|-
 
|-
 
|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.  
  
 
|-
 
|-
Line 81: Line 81:
 
|-
 
|-
 
|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.
  
 
|-
 
|-
Line 109: Line 109:
 
|-
 
|-
 
| 01:52
 
| 01:52
| This will open '''arrayIndex dot pl''' file in '''geditor'''
+
| This will open '''arrayIndex dot pl''' file in '''geditor'''.
 
   
 
   
 
|-
 
|-
Line 157: Line 157:
 
|-
 
|-
 
|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 an array length using a sample program.   
 
   
 
   
 
|-
 
|-
Line 177: Line 177:
 
|-
 
|-
 
| 03:18
 
| 03:18
| '''gedit arrayLength dot pl space ampersand '''
+
| '''gedit arrayLength dot pl space ampersand ''' and
 
   
 
   
 
|-
 
|-
 
|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.  
 
   
 
   
 
|-
 
|-
Line 197: Line 197:
 
|-
 
|-
 
|03:41
 
|03:41
|Highlighted are various ways to find the length of an array in Perl.  
+
|Highlighted are the various ways to find the length of an array in '''Perl'''.  
 
   
 
   
 
|-
 
|-
Line 229: Line 229:
 
|-
 
|-
 
|04:18
 
|04:18
|'''Indexing''' is used to access elements of an array.  
+
|Indexing is used to access elements of an array.  
 
   
 
   
 
|-
 
|-
Line 245: Line 245:
 
|-
 
|-
 
|04:29
 
|04:29
|* Any position  
+
|* Any position.
 
   
 
   
 
|-
 
|-
Line 313: Line 313:
 
|-
 
|-
 
|  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.
  
 
|-
 
|-
Line 325: Line 325:
 
|-
 
|-
 
|  05:56
 
|  05:56
|* Using '''for loop '''
+
|* Using '''for''' loop
 
    
 
    
 
|-
 
|-
 
|  05:58
 
|  05:58
|* Using '''foreach loop '''
+
|* Using '''foreach''' loop.
  
 
|-
 
|-
Line 353: Line 353:
 
|-
 
|-
 
| 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.  
  
 
|-
 
|-
Line 373: Line 373:
 
|-
 
|-
 
|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.  
  
 
|-
 
|-
Line 405: Line 405:
 
|-
 
|-
 
|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.  
  
 
|-
 
|-
Line 422: Line 422:
 
|-
 
|-
 
|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:
  
 
|-
 
|-
Line 446: Line 446:
 
|-
 
|-
 
|08:55
 
|08:55
|* Find index of an array  
+
|* Find '''index''' of an array  
  
 
|-
 
|-
Line 454: Line 454:
 
|-
 
|-
 
|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
|* Sequential Array  
+
|* '''Sequential Array''' and
  
 
|-
 
|-
 
|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 an 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'''.  
  
 
|-
 
|-
Line 514: Line 514:
 
|-
 
|-
 
|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.
 
   
 
   
 
|-
 
|-

Revision as of 15:47, 14 May 2015

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 and
00:20 * Array Slicing.
00:22 Here, I am using Ubuntu Linux 12.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 an array length using a sample program.
03:14 Switch to the terminal and type:
03:18 gedit arrayLength dot pl space ampersand and
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 the 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 * Sequential Array and
09:05 * Array Slicing, using sample programs.
09:07 Here is an 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