Difference between revisions of "PERL/C2/Arrays/English-timed"
From Script | Spoken-Tutorial
PoojaMoolya (Talk | contribs) |
PoojaMoolya (Talk | contribs) |
||
(11 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
{| border=1 | {| border=1 | ||
− | + | |'''Time''' | |
− | + | |'''Narration''' | |
|- | |- | ||
− | | 00 | + | | 00:01 |
|Welcome to the spoken tutorial on '''Arrays''' in '''Perl.''' | |Welcome to the spoken tutorial on '''Arrays''' in '''Perl.''' | ||
|- | |- | ||
− | | 00 | + | | 00:06 |
− | | In this tutorial, we will learn about | + | | In this tutorial, we will learn about: |
|- | |- | ||
− | |00 | + | |00:09 |
− | |'''Index''' of an | + | |'''Index''' of an array |
|- | |- | ||
− | |00 | + | |00:11 |
− | | Length of an | + | |Length of an array |
− | + | ||
|- | |- | ||
− | |00 | + | |00:13 |
− | |Accessing | + | |Accessing elements of an array |
|- | |- | ||
− | | 00 | + | | 00:16 |
− | | Looping over an | + | |Looping over an array |
|- | |- | ||
− | | 00 | + | | 00:18 |
− | |'''Sequential Array''' | + | | '''Sequential Array''' and |
|- | |- | ||
− | | 00 | + | | 00:20 |
− | | | + | |'''Array Slicing'''. |
− | + | ||
|- | |- | ||
− | |00 | + | |00:22 |
− | |Here I am using ''' Ubuntu | + | |Here, I am using ''' Ubuntu Linux 12.04''' operating system and '''Perl 5.14.2'''. |
|- | |- | ||
− | |00 | + | |00:30 |
|I will also be using the '''gedit''' Text Editor. | |I will also be using the '''gedit''' Text Editor. | ||
|- | |- | ||
− | | 00 | + | | 00:34 |
| You can use any text editor of your choice. | | You can use any text editor of your choice. | ||
|- | |- | ||
− | |00 | + | |00:37 |
− | |You should have basic knowledge of ''' | + | |You should have basic knowledge of ''' variables, comments & data Structures''' in '''Perl'''. |
|- | |- | ||
− | |00 | + | |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 | + | |00:48 |
− | |Please go through the relevant spoken tutorials on the ''' | + | |Please go through the relevant spoken tutorials on the '''Spoken Tutorial''' website. |
|- | |- | ||
− | |00 | + | |00:54 |
− | | | + | |Array is a simple '''data structure''' which contains '''elements''' of any data type. |
|- | |- | ||
− | |00 | + | |00:59 |
|'''Array index''' starts from zero always. | |'''Array index''' starts from zero always. | ||
− | |||
|- | |- | ||
− | | 01 | + | | 01:03 |
− | | In '''Perl''', it is not necessary to declare the length of an | + | | In '''Perl''', it is not necessary to declare the length of an array. |
|- | |- | ||
− | |01 | + | |01:08 |
− | | | + | |Array length expands/shrinks as and when elements are added/removed from it. |
|- | |- | ||
− | |01 | + | |01:15 |
− | | The syntax for declaring an array is | + | | The syntax for declaring an array is: |
|- | |- | ||
− | |01 | + | |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 | + | |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 | + | |01:35 |
|'''$#myArray ''' | |'''$#myArray ''' | ||
|- | |- | ||
− | |01 | + | |01:38 |
| Let us understand this using sample program. | | Let us understand this using sample program. | ||
|- | |- | ||
− | | 01 | + | | 01:42 |
− | |Open the terminal and type | + | |Open the terminal and type: |
|- | |- | ||
− | |01 | + | |01:44 |
|'''gedit arrayIndex dot pl space ampersand ''' | |'''gedit arrayIndex dot pl space ampersand ''' | ||
|- | |- | ||
− | |01 | + | |01:50 |
| and press '''Enter. ''' | | and press '''Enter. ''' | ||
− | |||
|- | |- | ||
− | | 01 | + | | 01:52 |
− | | This will open '''arrayIndex dot pl''' file in '''geditor''' | + | | This will open '''arrayIndex dot pl''' file in '''geditor'''. |
|- | |- | ||
− | |01 | + | |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 |
− | |Here we have declared & defined an | + | |Here, we have declared & defined an array which contains 5 elements. |
|- | |- | ||
− | |02 | + | |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 | + | |02:14 |
− | |i.e number of elements | + | |i.e number of elements which is 5, minus 1. |
|- | |- | ||
− | |02 | + | |02:18 |
− | |Press Ctrl+S to save the file. | + | |Press '''Ctrl+S''' to '''save''' the file. |
|- | |- | ||
− | |02 | + | |02:22 |
− | |Now switch to terminal and execute the Perl script | + | |Now, switch to terminal and execute the Perl script. |
|- | |- | ||
− | |02 | + | |02:26 |
− | | Type ''' perl arrayIndex dot pl ''' | + | | Type: ''' perl arrayIndex dot pl ''' |
|- | |- | ||
− | |02 | + | |02:30 |
|and press '''Enter.''' | |and press '''Enter.''' | ||
|- | |- | ||
− | |02 | + | |02:32 |
− | |The output will be as displayed on the terminal | + | |The output will be as displayed on the terminal. |
− | |||
− | |||
|- | |- | ||
− | |02 | + | |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 | + | |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 | + | |02:46 |
− | |Index of an | + | | Index of an array + 1 i.e. '''$#array + 1'''. |
|- | |- | ||
− | |02 | + | |02:53 |
− | |Using PERL inbuilt | + | |Using PERL inbuilt scalar function; i.e. '''scalar open bracket @array close bracket.''' |
− | |||
|- | |- | ||
− | |03 | + | |03:02 |
− | |Assign array to a | + | |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. | + | | Let us look at an illustration of an array length using a sample program. |
|- | |- | ||
− | | | + | |03:14 |
− | | Switch to the terminal and type | + | | Switch to the terminal and type: |
− | + | ||
|- | |- | ||
− | | 03 | + | |03:18 |
− | | '''gedit arrayLength dot pl space ampersand ''' | + | | '''gedit arrayLength dot pl space ampersand ''' and |
|- | |- | ||
− | |03 | + | |03:24 |
− | | | + | |press '''Enter.''' |
|- | |- | ||
− | | 03 | + | | 03:27 |
− | | Type the following piece of code | + | | Type the following piece of code as shown on the screen. |
|- | |- | ||
− | | 03 | + | | 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 | + | |03:38 |
|So, output will display 5. | |So, output will display 5. | ||
|- | |- | ||
− | |03 | + | |03:41 |
− | |Highlighted | + | |Highlighted are the various ways to find the length of an array in '''Perl'''. |
|- | |- | ||
− | | 03 | + | | 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 | + | |03:53 |
− | |Press '''Ctrl + S''' to save the file. | + | |Press '''Ctrl + S''' to '''save''' the file. |
− | + | ||
− | + | ||
|- | |- | ||
− | | 03 | + | | 03:57 |
− | | Now let us execute the script. | + | | Now, let us execute the script. |
|- | |- | ||
− | | 03 | + | | 03:59 |
− | | Switch to terminal and type | + | | Switch to terminal and type: |
− | |||
|- | |- | ||
− | | 04 | + | | 04:02 |
| '''perl arrayLength dot pl''' and press '''Enter.''' | | '''perl arrayLength dot pl''' and press '''Enter.''' | ||
|- | |- | ||
− | | 04 | + | | 04:07 |
| The output will be as displayed on the terminal. | | The output will be as displayed on the terminal. | ||
|- | |- | ||
− | | 04 | + | | 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 | + | |04:18 |
− | | | + | |Indexing is used to access elements of an array. |
− | + | ||
|- | |- | ||
− | |04 | + | |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 | + | |04:27 |
− | |First Position | + | |First Position, Last Position |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
− | |04 | + | |04:29 |
− | |Any position | + | |Any position. |
|- | |- | ||
− | | 04 | + | | 04:32 |
− | | Switch to the terminal and type | + | | Switch to the terminal and type: |
|- | |- | ||
− | | 04 | + | | 04:35 |
| '''gedit perlArray dot pl space ampersand ''' | | '''gedit perlArray dot pl space ampersand ''' | ||
|- | |- | ||
− | |04 | + | |04:42 |
|and press '''Enter.''' | |and press '''Enter.''' | ||
− | |||
|- | |- | ||
− | |04 | + | |04:45 |
|Type the following piece of code as shown. | |Type the following piece of code as shown. | ||
|- | |- | ||
− | |04 | + | |04:49 |
− | |Please note | + | |Please note: '''myArray''' is declared with '''@''' (at the rate) sign. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
− | |04 | + | |04:54 |
− | |But, to access an array element we need to use ''' $ | + | |But, to access an array element we need to use '''$'''(dollar) sign. |
|- | |- | ||
− | |04 | + | |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 | + | |05:07 |
− | |Here, to access the first element of '''myArray''' , | + | |Here, to access the first element of '''myArray''', |
− | |||
|- | |- | ||
− | |05 | + | |05:11 |
− | |zero is passed as | + | |zero is passed as index. |
|- | |- | ||
− | | 05 | + | | 05:16 |
− | |To access the last element of '''myArray''' , we have passed the last | + | |To access the last element of '''myArray''' , we have passed the last index of '''myArray.''' |
|- | |- | ||
− | | 05 | + | | 05:24 |
| Recall, we had learnt about this earlier. | | Recall, we had learnt about this earlier. | ||
− | |||
|- | |- | ||
− | |05 | + | |05:28 |
− | | Press Ctrl + S to save the file. | + | | Press '''Ctrl + S''' to '''save''' the file. |
− | |||
|- | |- | ||
− | | 05 | + | | 05:30 |
− | | Switch to the terminal and execute the script as | + | | Switch to the terminal and execute the script as: |
− | + | ||
|- | |- | ||
− | | 05 | + | | 05:36 |
| '''perl perlArray dot pl''' | | '''perl perlArray dot pl''' | ||
|- | |- | ||
− | | 05 | + | | 05:41 |
| and press '''Enter.''' | | and press '''Enter.''' | ||
|- | |- | ||
− | | 05 | + | | 05:43 |
− | | The output will be as shown on the terminal | + | | The output will be as shown on the terminal. |
− | + | ||
|- | |- | ||
− | | 05 | + | | 05:47 |
− | | Now, let us understand | + | | Now, let us understand how to '''loop''' over each element of an array. |
|- | |- | ||
− | |05 | + | |05:52 |
− | |There are two ways of '''looping over''' an array | + | |There are two ways of '''looping over''' an array: |
− | + | ||
|- | |- | ||
− | | 05 | + | | 05:56 |
− | | | + | |Using '''for''' loop |
− | + | ||
− | + | ||
|- | |- | ||
− | | 05 | + | | 05:58 |
− | | Using '''foreach | + | |Using '''foreach''' loop. |
|- | |- | ||
− | |06 | + | |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 | + | | 06:07 |
− | |For this, switch to the terminal and type | + | |For this, switch to the terminal and type: |
|- | |- | ||
− | |06 | + | |06:11 |
| '''gedit loopingOverArray dot pl space ampersand ''' | | '''gedit loopingOverArray dot pl space ampersand ''' | ||
− | |||
|- | |- | ||
− | |06 | + | |06:17 |
− | | | + | |and press '''Enter'''. |
− | + | ||
|- | |- | ||
− | | 06 | + | | 06:20 |
− | | Type the piece of code as shown on the screen | + | | Type the piece of code as shown on the screen. |
|- | |- | ||
− | | 06 | + | | 06:24 |
− | | Here, we are printing each element of | + | | Here, we are printing each element of array by iterating the index. |
|- | |- | ||
− | | 06 | + | | 06:31 |
− | | The '''for | + | | The '''for''' loop will execute till the value of 'i' variable reaches the last '''index''' of an array. |
|- | |- | ||
− | | 06 | + | | 06:38 |
− | |Here, '''foreach | + | |Here, '''foreach''' loop will be executed for each element of an array. |
− | + | ||
|- | |- | ||
− | |06 | + | |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 | + | |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 | + | |06:58 |
− | + | |please go through the relevant spoken tutorials on "spoken tutorial" website. | |
|- | |- | ||
− | |07 | + | |07:04 |
− | | Now, | + | | Now, press '''Ctrl + S''' to '''save''' the file. |
|- | |- | ||
− | |07 | + | |07:07 |
− | |Then switch to the terminal and execute the script as | + | |Then switch to the terminal and execute the script as: |
|- | |- | ||
− | |07 | + | |07:12 |
| '''perl loopingOverArray dot pl''' | | '''perl loopingOverArray dot pl''' | ||
|- | |- | ||
− | |07 | + | |07:15 |
| and press '''Enter.''' | | and press '''Enter.''' | ||
|- | |- | ||
− | |07 | + | |07:19 |
|The output will be as displayed on the terminal. | |The output will be as displayed on the terminal. | ||
|- | |- | ||
− | |07 | + | |07:24 |
− | | In '''Perl''', we can declare a '''sequential array''' as | + | | In '''Perl''', we can declare a '''sequential array''' as: |
|- | |- | ||
− | |07 | + | |07:28 |
− | |'''@alphaArray | + | |'''@alphaArray = open bracket a dot dot d close bracket semicolon''' |
|- | |- | ||
− | |07 | + | |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 | + | |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 | + | |
− | + | ||
|- | |- | ||
− | |08 | + | |08:03 |
− | |Perl also provides array slicing. | + | |'''Perl''' also provides '''array slicing'''. |
|- | |- | ||
− | |08 | + | |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 | + | |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 | + | |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 | + | |08:38 |
− | |After slicing, the newArray will look like | + | |After '''slicing''', the '''newArray''' will look like: |
|- | |- | ||
− | | | + | |08:42 |
|'''@newArray''' = open bracket 23 comma 87 close bracket semicolon | |'''@newArray''' = open bracket 23 comma 87 close bracket semicolon | ||
− | |||
− | |||
− | |||
− | |||
− | |||
|- | |- | ||
− | |08 | + | |08:51 |
− | |In this tutorial, we have learnt to | + | |Let us summarize. In this tutorial, we have learnt to: |
|- | |- | ||
− | |08 | + | |08:55 |
− | |Find index of an array | + | |Find '''index''' of an array |
|- | |- | ||
− | |08 | + | |08:57 |
|Find length of an array | |Find length of an array | ||
|- | |- | ||
− | |08 | + | |08:59 |
− | |Access elements of an array | + | |'''Access''' elements of an array |
|- | |- | ||
− | |09 | + | |09:01 |
− | |Loop over an array | + | |'''Loop''' over an array |
|- | |- | ||
− | |09 | + | |09:03 |
− | | | + | |'''Sequential Array''' and |
|- | |- | ||
− | |09 | + | |09:05 |
− | |Array Slicing using sample programs. | + | |'''Array Slicing''', using sample programs. |
|- | |- | ||
− | |09 | + | |09:07 |
− | |Here is assignment for you | + | |Here is an assignment for you: |
|- | |- | ||
− | |09 | + | |09:10 |
− | |Declare an array of rainbow colors | + | | Declare an array of rainbow colors. |
|- | |- | ||
− | |09 | + | |09:13 |
− | |Print 4th element of this array | + | | Print 4th element of this array. |
|- | |- | ||
− | |09 | + | |09:16 |
− | |Print Length and last index of this array | + | | Print Length and last index of this array. |
|- | |- | ||
− | |09 | + | |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 | + | |09:25 |
− | |Declare array as @myArray = open bracket 1..9 close bracket semicolon | + | | 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 | + | |09:41 |
− | |Watch the video available at the following link | + | |Watch the video available at the following link. |
− | + | ||
|- | |- | ||
− | |09 | + | |09:44 |
− | |It summaries the Spoken Tutorial project | + | |It summaries the Spoken Tutorial project. |
− | + | ||
− | + | ||
|- | |- | ||
− | |09 | + | |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 | + | | 09:53 |
− | | The Spoken Tutorial | + | | The Spoken Tutorial project team: * Conducts workshops using spoken tutorials. |
− | + | ||
|- | |- | ||
− | |09 | + | |09:58 |
− | |Gives certificates to those who pass an online | + | | Gives certificates to those who pass an online test. |
− | + | ||
|- | |- | ||
− | |10 | + | |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 | + | | 10:09 |
− | | Spoken Tutorial | + | | "Spoken Tutorial" project is a part of the "Talk to a Teacher" project. |
− | + | ||
− | + | ||
|- | |- | ||
− | |10 | + | |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 | + | |10:20 |
− | |More information on this | + | |More information on this mission is available at: spoken hyphen tutorial dot org slash NMEICT hyphen Intro. |
|- | |- | ||
− | | 10 | + | | 10:31 |
− | | | + | | Hope you enjoyed this '''Perl''' tutorial. |
|- | |- | ||
− | |10 | + | |10:35 |
− | |This is Amol signing off. | + | |This is Amol, signing off. |
− | + | ||
− | + | ||
|- | |- | ||
− | |10 | + | |10:37 |
|Thanks for joining. | |Thanks for joining. | ||
|} | |} |
Latest revision as of 15:43, 10 March 2017
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, 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: 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. 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. |