1 00:00:00 --> 00:00:04 Welcome to the spoken tutorial on Data Structures in Perl 2 00:00:05 --> 00:00:10 In this tutorial, we will learn about Data Structures available in Perl 3 00:00:11 --> 00:00:17 Here I am using Ubuntu Linux12.04 operating system and Perl 5.14.2 4 00:00:18 --> 00:00:21 I will also be using the gedit Text Editor. 5 00:00:22 --> 00:00:24 You can use any text editor of your choice. 6 00:00:25 --> 00:00:28 You should have basic knowledge of Variables in Perl 7 00:00:29 --> 00:00:35 Knowledge of comments, loops and conditional statements will be an added advantage. 8 00:00:36 --> 00:00:40 Please go through the relevant spoken tutorials on the spoken tutorial website. 9 00:00:41 --> 00:00:43 Perl has 3 types of data structure - 10 00:00:44 --> 00:00:44 Scalar 11 00:00:45 --> 00:00:45 Array 12 00:00:46 --> 00:00:49 Hash , also, called as Associative Array 13 00:00:50 --> 00:00:55 Scalar: This type of data structure holds a value of any data type. 14 00:00:56 --> 00:01:00 The data type can be string, number, double etc. 15 00:01:01 --> 00:01:05 It can also hold the reference to an array or reference to a hash. 16 00:01:06 --> 00:01:10 Note: Reference in Perl will be covered in subsequent tutorial. 17 00:01:11 --> 00:01:15 Scalar type of data structure is as simple as declaring the variable. 18 00:01:16 --> 00:01:19 $count = 12 semicolon 19 00:01:20 --> 00:01:25 $string = in single quote 'I am scalar of type string' semicolon. 20 00:01:26 --> 00:01:29 We can perform the following operations on scalar 21 00:01:30 --> 00:01:31 Assign a value to it 22 00:01:32 --> 00:01:34 Assign one scalar to another 23 00:01:35 --> 00:01:40 Arithmetic operations on number type of scalars like add, subtract etc 24 00:01:41 --> 00:01:47 string operations on string scalar like concatenation, substr etc 25 00:01:48 --> 00:01:51 Now let us look at an example of scalar data structure. 26 00:01:52 --> 00:02:00 Switch to terminal and type gedit scalars dot pl space & and press Enter. 27 00:02:01 --> 00:02:04 This will open the scalars dot pl file in gedit. 28 00:02:05 --> 00:02:08 Type the following code as displayed on the screen. 29 00:02:09 --> 00:02:12 This is the declaration and assignment to the scalar. 30 00:02:13 --> 00:02:18 These are few arithmetic operations that can be performed on number type of scalar 31 00:02:19 --> 00:02:24 These are string operations that can be performed on string type of scalar. 32 00:02:25 --> 00:02:29 substr is the PERL function which provides part of the string as output. 33 00:02:30 --> 00:02:38 Here index 0 specifies start of a string, i.e. from where we want to start extraction of the string. 34 00:02:39 --> 00:02:45 And 11 specify the offset upto where we want the string to be in the output. 35 00:02:46 --> 00:02:49 Press ctrl + s to save the file. 36 00:02:50 --> 00:02:54 Then switch to the terminal and execute the Perl script as 37 00:02:55 --> 00:02:59 perl scalars dot pl and press Enter 38 00:03:00 --> 00:03:04 The output shown on terminal is as highlighted 39 00:03:05 --> 00:03:08 Now, let us look at array data structure in PERL. 40 00:03:09 --> 00:03:11 Array: It is a list of elements. 41 00:03:12 --> 00:03:15 Elements can be string, number etc. 42 00:03:16 --> 00:03:21 It has an index, which is used for performing various operations on the array. 43 00:03:22 --> 00:03:24 Index starts with zero. 44 00:03:25 --> 00:03:32 Unlike other programming languages, there is no need to declare an array or its length before using it in Perl. 45 00:03:33 --> 00:03:38 Perl array, stretches or shrinks as per the elements added or removed from it 46 00:03:39 --> 00:03:40 The syntax to write an array is; 47 00:03:41 --> 00:03:53 at the rate variableName space equal to space open bracket list of elements separated with comma close bracket semicolon 48 00:03:54 --> 00:03:56 Now let us look at an example of array data structure. 49 00:03:57 --> 00:04:07 Switch to terminal and type gedit perlArray dot pl space & and press Enter. 50 00:04:08 --> 00:04:11 This will open the perlArray dot pl file in gedit. 51 00:04:12 --> 00:04:17 Type the following code as displayed on the screen. 52 00:04:18 --> 00:04:22 This is the number array which has elements of number type. 53 00:04:23 --> 00:04:28 This is the string array which has elements of string type. 54 00:04:29 --> 00:04:33 This array has elements of both number and string type. 55 00:04:34 --> 00:04:38 This example shows the various types of arrays in Perl. 56 00:04:39 --> 00:04:42 This is how we can print the array in Perl. 57 00:04:43 --> 00:04:46 Press Ctrl + S to save the file. 58 00:04:47 --> 00:04:51 Then switch to terminal and execute the Perl script as 59 00:04:52 --> 00:04:58 perl perlArray dot pl and press Enter. 60 00:04:59 --> 00:05:03 The following output is displayed on the terminal 61 00:05:04 --> 00:05:07 Now, let us look at Hash data structure in Perl. 62 00:05:08 --> 00:05:11 Hash is alternatively called as Associative array 63 00:05:12 --> 00:05:14 It is a Key Value pair data structure. 64 00:05:15 --> 00:05:17 Key in hash is unique. 65 00:05:18 --> 00:05:27 If the same key is added again, then the value of that key will be overridden by the latest value assigned to the key. 66 00:05:28 --> 00:05:29 Value can be duplicate. 67 00:05:30 --> 00:05:33 It also holds value of any data type. 68 00:05:34 --> 00:05:35 The syntax of hash is; 69 00:05:36 --> 00:05:40 percentage variable name space equal to space open bracket 70 00:05:41 --> 00:05:41 Press Enter 71 00:05:42 --> 00:05:49 single quote key Name single quote space equal to greater than sign space Value comma 72 00:05:50 --> 00:05:51 Press Enter 73 00:05:52 --> 00:05:57 single quote key Name single quote space equal to greater than sign space Value 74 00:05:58 --> 00:05:59 Press Enter 75 00:06:00 --> 00:06:02 close bracket semicolon 76 00:06:03 --> 00:06:06 Now let us look at an example of hash data structure. 77 00:06:07 --> 00:06:09 Switch to terminal and type 78 00:06:10 --> 00:06:17 gedit perlHash dot pl space & and press Enter. 79 00:06:18 --> 00:06:21 This will open the perlHash dot pl file in gedit. 80 00:06:22 --> 00:06:26 Type the following code as displayed on the screen. 81 00:06:27 --> 00:06:30 This hash indicates the marks obtained in a subject. 82 00:06:31 --> 00:06:34 This example, shows the use of hash 83 00:06:35 --> 00:06:37 Now let us see how to print the hash 84 00:06:38 --> 00:06:41 For now, just note the way I have printed the hash. 85 00:06:42 --> 00:06:46 Detailed explanation will be given in subsequent tutorial. 86 00:06:47 --> 00:06:49 Press Ctrl + S to save the file. 87 00:06:50 --> 00:06:54 Then switch to terminal and execute the Perl script as 88 00:06:55 --> 00:07:00 perl perlHash dot pl and press Enter. 89 00:07:01 --> 00:07:04 The following output is displayed on the terminal 90 00:07:05 --> 00:07:05 Let us summarize. 91 00:07:06 --> 00:07:08 In this tutorial, we have learnt - 92 00:07:09 --> 00:07:09 scalar 93 00:07:10 --> 00:07:10 Array and 94 00:07:11 --> 00:07:12 Hash Data Structure in Perl 95 00:07:13 --> 00:07:14 using sample programs. 96 00:07:15 --> 00:07:16 There is assignment for you - 97 00:07:17 --> 00:07:18 Declare scalar variable 98 00:07:19 --> 00:07:22 Assign value of type float to it and then print it. 99 00:07:23 --> 00:07:27 Declare and print an array of colors 'Red', 'Yellow' and 'Green'. 100 00:07:28 --> 00:07:32 Declare and print a hash of Employee Name and their department. 101 00:07:33 --> 00:07:37 Hint: 'Employee' =>(equal to greater than sign) 'John' comma 102 00:07:38 --> 00:07:41 'Department' =>(equal to greater than sign) 'Engineering' 103 00:07:42 --> 00:07:45 Watch the video available at the following link 104 00:07:46 --> 00:07:48 It summaries the Spoken Tutorial project 105 00:07:49 --> 00:07:52 If you do not have good bandwidth, you can download and watch it 106 00:07:53 --> 00:07:58 The Spoken Tutorial Project Team Conducts workshops using spoken tutorials 107 00:07:59 --> 00:08:02 Gives certificates to those who pass an online test 108 00:08:03 --> 00:08:09 For more details, please write to contact at spoken hyphen tutorial dot org 109 00:08:10 --> 00:08:14 Spoken Tutorial Project is a part of the Talk to a Teacher project 110 00:08:15 --> 00:08:21 It is supported by the National Mission on Education through ICT, MHRD, Government of India. 111 00:08:22 --> 00:08:32 More information on this Mission is available at spoken hyphen tutorial dot org slash NMEICT hyphen Intro 112 00:08:33 --> 00:08:34 Hope you enjoyed this Perl tutorial. 113 00:08:35 --> 00:08:37 This is Amol signing off. 114 00:08:38 --> 00:08:43 Thanks for joining.