PERL/C2/Array-functions/English-timed

From Script | Spoken-Tutorial
Revision as of 11:27, 10 July 2014 by Gaurav (Talk | contribs)

Jump to: navigation, search
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
00:17 and join
00:18 sort
00:19 qw
00:20 I am using Ubuntu Linux12.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 works at first position of an Array.
04:27 We can collect the element removed by shift function into some variable.
04:33 This 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 upto 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 needs 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 an 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 summaries 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.

Contributors and Content Editors

Gaurav, PoojaMoolya, Sandhya.np14