Difference between revisions of "PERL/C2/Array-functions/English-timed"
From Script | Spoken-Tutorial
Sandhya.np14 (Talk | contribs) |
Sandhya.np14 (Talk | contribs) |
||
Line 5: | Line 5: | ||
|- | |- | ||
| 00:01 | | 00:01 | ||
− | |Welcome to the spoken tutorial on '''Array Functions''' in ''' | + | |Welcome to the spoken tutorial on '''Array Functions''' in '''PERL'''. |
|- | |- | ||
| 00:06 | | 00:06 | ||
− | | In this tutorial, we will learn about '''Array functions''' in ''' | + | | In this tutorial, we will learn about '''Array functions''' in '''PERL''' like: |
|- | |- |
Revision as of 11:01, 13 May 2015
Time | Narration |
00:01 | Welcome to the spoken tutorial on Array Functions in PERL. |
00:06 | In this tutorial, we will learn about Array functions in PERL like: |
00:11 | * push |
00:11 | * pop |
00:12 | * shift |
00:14 | * unshift |
00:15 | * split |
00:16 | * splice and |
00:17 | * join |
00:18 | * sort |
00:19 | * qw. |
00:20 | I am using Ubuntu Linux 12.04 operating system and Perl 5.14.2. |
00:28 | I will also be using the gedit Text Editor. |
00:32 | You can use any text editor of your choice. |
00:36 | You should have basic knowledge of variables, Data Structures and Arrays in Perl. |
00:43 | Knowledge of Comments, 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 | Perl provides certain inbuilt functions. |
00:57 | These functions can perform various operations on an array. |
01:02 | Let us first learn how to add and remove elements from last position of an array. |
01:08 | This can be done by using: |
01:10 | push function which adds an element at the last position of an array |
01:15 | and pop function which removes an element from the last position of an array. |
01:21 | Let us understand push and pop functions by using a sample program. |
01:26 | Open the terminal and type: gedit perlArray dot pl space ampersand |
01:33 | and press Enter. |
01:36 | This will open 'perlArray dot pl' file in gedit. |
01:41 | Type the code as shown on the screen. |
01:45 | Here, we have declared an array of length 3. |
01:50 | push function will insert an element at the last position of this array i.e after 3 |
01:57 | whereas pop function will remove an element from the last position of the array. |
02:04 | In our case, 4 will be removed from the array. |
02:08 | Press Ctrl + S to save the file. |
02:11 | push function takes 2 arguments- |
02:14 | 1st argument to the push function is the array in which to add an element. |
02:20 | 2nd argument is the element which is to be pushed into the array. |
02:25 | The syntax of pop function is as follows: |
02:29 | pop function takes only one argument. |
02:32 | It is the array from which an element needs to be removed. |
02:36 | Note: Both these functions work at last position of an array. |
02:41 | The element removed by pop function can be collected into another variable. |
02:46 | The syntax for this is $variable space = space pop open bracket @myArray close bracket . |
02:57 | Now switch to the terminal and execute the Perl script. |
03:01 | Type: perl perlArray dot pl and press Enter. |
03:07 | The output is as shown on the terminal. |
03:11 | Now, let us see how to add/remove an element from the 1st position of an array. |
03:18 | This can be achieved using- |
03:20 | unshift function which adds an element to an array at the 1st position. |
03:25 | shift function which removes the first element from an array. |
03:31 | Let us understand this using a sample program. |
03:35 | I will open perlArray dot pl file which I have already created. |
03:39 | Type the following piece of code as shown on the screen. |
03:43 | unshift function will insert an element at the first position i.e before 1. |
03:52 | shift function will remove an element from the first position. |
03:57 | In our case, zero will be removed. |
04:00 | Press Ctrl + S to save the file. |
04:03 | The unshift function takes 2 arguments- |
04:06 | 1st argument is the array in which to add an element, |
04:10 | 2nd argument is the element to be added into the array . |
04:15 | shift function takes only one argument - |
04:18 | This is the array from which the element needs to be removed. |
04:22 | Note: Both these functions work at first position of an array. |
04:27 | We can collect the element removed by shift function into some variable. |
04:33 | The syntax for this is: $variable space = space shift open bracket @myArray close bracket. |
04:44 | Then switch to the terminal and execute the Perl script. |
04:48 | Type: perl perlArray dot pl and press Enter. |
04:54 | The output displayed on the terminal is as highlighted. |
04:59 | Now, let us see how to remove an element from a specified position of an array. |
05:05 | splice function removes an element from a specified position of an array. |
05:11 | The return value of this function is an array of removed elements. |
05:17 | Let us understand this using a sample program. |
05:21 | Go to the perlArray dot pl file which we created earlier. |
05:26 | Type the piece of code as shown on the screen. |
05:30 | We need to provide index from where we want to remove the elements and |
05:35 | the offset up to which we want to remove the elements . |
05:39 | In our case, the elements 5 and 6 will be removed. |
05:44 | Then switch to the terminal and execute the Perl script by typing: |
05:49 | perl perlArray dot pl and press Enter. |
05:55 | The output is as shown on the terminal. |
05:59 | Now, let us look at few other inbuilt functions of arrays. |
06:04 | split function is used to divide a string at a specified delimiter. |
06:10 | The return value of this function is an array. |
06:14 | The elements of this array are the divided portions of the string. |
06:19 | join function joins the elements of an array using the specified delimiter. |
06:25 | It returns a string of joined elements. |
06:28 | sort function sorts an array in alphabetical/numerical order. |
06:34 | qw function returns an array of words, separated by a white space. |
06:40 | Now let us understand all these functions using a sample program. |
06:45 | Switch to terminal and type: |
06:48 | gedit arrayFunctions dot pl space ampersand and press Enter. |
06:55 | Type the following piece of code as shown on screen. |
07:00 | In this case, each word of the variable string will become an element of an Array. |
07:07 | Here, each element of newArray will be joined by comma. |
07:12 | sort function will sort the elements of an array in alphabetical order. |
07:19 | qw function creates an array of words separated by space. |
07:25 | Let us understand each function. |
07:28 | split function takes two arguments. |
07:31 | 1st argument is the delimiter by which the string needs to be split, |
07:36 | 2nd is the string which needs to be split. |
07:39 | Delimiters can be specified in forward slash, single or double quotes. |
07:45 | join function takes 2 arguments. |
07:48 | 1st is the delimiter by which the array elements need to be joined. |
07:53 | 2nd is the array. |
07:55 | Delimiters can be specified in single or double quotes. |
07:58 | sort function takes a single argument which is the array that needs to be sorted. |
08:05 | qw function returns an array of words, separated by space. |
08:11 | It is not necessary to specify the word in quotes if written using qw. |
08:17 | Then switch to the terminal and execute the Perl script by typing: |
08:23 | perl arrayFunctions dot pl |
08:26 | and press Enter. |
08:29 | The following output is displayed on the screen. |
08:33 | Let us summarize. |
08:34 | In this tutorial, we have learnt to: |
08:36 | * add/remove elements from an array |
08:40 | * basic functions which can be performed on array using sample programs. |
08:46 | Here is assignment for you - |
08:48 | 'script.spoken-tutorial.org/index.php/Perl' |
08:54 | * split the above string at '/ ' (forward slash) delimiter |
08:59 | * Add https:// at the start of a newly created array |
09:06 | * Remove element “Perl” from the array |
09:09 | * Declare number array and sort it. |
09:12 | Watch the video available at the following link. |
09:15 | It summarizes the "Spoken Tutorial" project. |
09:19 | If you do not have good bandwidth, you can download and watch it. |
09:24 | The "Spoken Tutorial" project team: Conducts workshops using spoken tutorials. |
09:30 | Gives certificates to those who pass an online test. |
09:34 | For more details, please write to: contact at spoken hyphen tutorial dot org. |
09:40 | "Spoken Tutorial" project is a part of the "Talk to a Teacher" project. |
09:44 | It is supported by the National Mission on Education through ICT, MHRD, Government of India. |
09:51 | More information on this mission is available at: spoken hyphen tutorial dot org slash NMEICT hyphen Intro. |
10:02 | Hope you enjoyed this PERL tutorial. |
10:04 | This is Amol, signing off. |
10:06 | Thanks for joining. |