 Hi everyone once again welcome to this lecture we are actually the course is the next programming and scripting we have been talking about the Perl programming language and today is Perl lecture seven we have made a lot of progress with respect to Perl and I hope you are now much more familiar with the inner workings of Perl and are confident to do programming in Perl but mainly like I mean we looked at several data structures so far and we will continue our discussions still with the data structures in Perl then we will take it take upon the controls while we are doing this itself we are learning somewhat some of the controls so it is really good actually so let us recap as to what we saw in the last class so in the last class we continued the discussions on array we looked at some operations on array for example the reverse how to reverse the array essentially like the order of the array using the reverse function you also understood about the sort function sort and then we also did some chopping use the chop function to chop the last character of every element in the array then we also looked at the the data from the array context as well as a scalar complex and we noted the differences between how they are perceived how they will be perceived if you are in the array context and model the stable context we also studied how to avoid array reference for scalars and we studied principally like the three methods as to how to define a scalar when we use like the annotation essentially so a dollar variable followed by a square bracket is always an array that we know about but then how to distinguish between a dollar and basically a scalar variable with just a square bracket one method was we can define the variable mean under this the brackets of the double brackets essentially which is actually shown here so this is all this notation the other one is the concatenation notation there we can actually use a dot to actually concatenate variables essentially so that distinguishes completely like I mean between a scalar variable and square bracket or the third option is this one where we can escape the square brackets using a backslash I think like I mean this is pretty much like I mean what we talked about in the last lecture with this I think we are ready to move on what I want to really touch upon today is the associative array or the hash array and we will learn about that today then we will also see like I mean some of the common challenges the two main things in the pearl language if you do not know anything that you should really know about is one of them is the hash array and how to use the hash array and then the second one is the regular expression so we will see the regular expression in more detail in the coming classes I always promise you that so we are not we are not it compete with the pearl programming language so we will be looking at that but today we will talk about the associative array and the various things associated with that the functions and also how to use them and we will also look at some simple application essentially and how we can do it and I will also give you some problems to work on to understand the concepts of so without delaying further let's look at what it is essentially so a hash array couple of things one is it does not have a numerical index it uses the general data often strings or the index itself is referred to as key in a hash array and then the corresponding element is its value so these two terms are very important because we will be looking at this from these two terms so I want you to remember this a hash array or an associative array is always referred to the indexes are referred to as key and then the corresponding element in the array is referred to as value so the hash table basically like I mean so we use the hash table to implement the associative array so hence these structures are known as ashes in work. Hash is not probably like new to you like essentially like hash is something which is unique in the sense that it has a very random access capabilities built in and essentially I mean that's what we are striving to achieve when using the associative array so you don't have to do a linear search to get to a particular element instead you can directly go into that particular element using the hash function and so the other just resulting from that is that the elements in the Perl hash do not have any natural ordering so there is no relationship between each of the keys or each of the indexes in the array in the associative array whereas in a regular array as you know the numerical values are incremented one at a time starting from zero things like that basically like where we don't implicitly specify so the index itself so here there is no definite relationship between the order of the keys and either the values of the keys or the order in which they are entered into the hash so we just enter values and they can be attached anywhere within the array I will show you some examples essentially as to how we add how we delete objects from the hash array let's look at more things essentially couple of more syntax issues one is the the hash variables of the hash array the names begin with this character percentage if an array is assigned to have the even index elements becomes the keys and then the odd in index elements are the corresponding values so assigning an odd length array to a hash will cause an error so these are some of the basic things that you may want to look at meaning like I mean this is like I mean by mistake like I mean you are instead of ampersand you don't use that and instead you do for percentage and then the calibrated are used to subscript the hash so if the percentage H is an ash then the element correspond to a for you can do it as dollar H and then the for essentially so let's there is an example here so here this is a percentage hash essentially because three elements so this is what is this key and this is the value so again another key is Bob and that value is easy and then the percentage is a hash of 50 and the value is John so look at that I write I mean so he can be just a character string or a numerical value same thing with values essentially they can be just a number or string or any other data structures so so like I mean so this is this is the key thing basically we will they will see like I mean how we use this and how we will you know insert into this so let's look at so in a nutshell essentially again it is an unordered list unordered set of pairs of keys and values and each key needs to be associated with a value otherwise it is an error so we mentioned that basically it always starts with the percentage so here I am declaring my percentage hash this is again a declaration essentially like I mean this is my term in fact Perl does not impose any kind of restriction you can use variables as you please you do not have to declare but it is a it is a good idea and a good practice and a good style to actually declare your variables up front using the my notation again unlike other programming languages you do not have to specify whether it is an integer whether it is a string whether it is a character those kind of variables you can just simply say like my particular variable name so here the initialization essentially like I mean this is one way to initialize so basically assign the key aim to five the value five and then key bob to value ZZZ and then E 50 to value job so so far it is fairly easy declaring the variable and then basically a family now how do we access it so we can do like as I mentioned like a curly braces here so within the curly braces we specify the key so in order to access that particular value so it is a print $ hash 50 that gives you job and if you want to reset this hash table essentially like I mean you just move all these things although key value pairs and simply do this and then that will reset that so now let us look at how do we modify what happens when we say ash bob is a a a so now what happens to this one the value simply changed so from ZZZ it changed to a now if you want to add a new way new key value pair we just simply specify hash and then that particular key with what the value is and then this gets added so basically now the hash array becomes a the number of elements become four and then the the value that you mentioned is getting inserted please note that actually there is no order here I mean certain at the end it can be anywhere in the middle or wherever it is basically just gets added to this hash array that is how you should look at it because there is no order that is associated with it so you cannot say like hash and then you get this essentially like I mean you need to access it using the key and then you get the value so again there is no order in this assignment the way that certain key whether it exists use this function called exists so we can say like I mean this actually like a man is actually doing two things one is exists this essentially like I mean it is actually you know finding whether there is a value that exists corresponding to this key essentially which is kind of testing whether there is a key that is existing in that now we can also do a delete essentially like I mean that is simply just use the delete function and then we specify the particular key value pair the key value pair is denoted as just the array followed by the keys essentially like so the key and the value both get deleted in this case so for example here we say the 50 which is associated John so when we execute this both of them get deleted so it is not just it is removing the 50 or it is removing just John it is removing both the key and the value from that all right so now so far we have learned the three different data types essentially the variable types essentially one is the scale of scalar could be a number or a string the array data type which is essentially like a string of objects essentially so the array has an value of 0 which is the first one and then the last one is whatever go the entire whereas for a hash essentially hash has just so the value pair so notice that actually like I mean here the elements are all like going together because it starts from 0 1 2 3 and 4 etc whereas here there is no any order here essentially only connection is this element is connected there is this element that is all so you can think of hash array as a two-dimension arrays which is how we can make use of it in real world applications and then also like I mean it is you can also call it as a sparse array so this is again useful say for example like I mean you have a coordinate system where a point is denoted by XY and then you have given like I mean say like three points to identify these points you can actually build a hash array where you can say like I mean percentage points and then you can say like percentage points and then the key can be this x and that corresponds to say that this is x1 y1 this is x2 y2 and this is x3 y3 I can just simply note that x1 2 y1 x2 y2 x3 2 y3 now if I access like dollar points x1 this will get me the y1 so now you can see that actually like I can preserve both x1 and y1 and the points that are relevant for me I can store it within there so this is one way to actually store like a two-dimensional array piece with the relevance and then now I can do any kind of operations between them I know the x1 I know y1 I can get to x1 from y1 there is an interesting problem that I will post later on there I want you to get x1 given what is the y1 so for that I will give you like more functions essentially so let us look at some more functions to work with hash arrays so here another practical example is essentially a phone book we can associate various users with their phone numbers and put them in a hash array and then when we say like phone book for a given user it prints their numbers this is pretty much what we saw basically like how we change the hash array the values can be assigned to a hash reference to insert a new key value relation or change the value related to the key these two we saw already a key value relation can be removed with the delete operator this is something that we saw already and then this is another way to delete all the contents of the hash array we know that one another method is the percentage hash equal to yes this will get rid of everything the other operators is basically like under percentage hash this also will just remove all the contents from the hash and then the exit operator this is something that we saw basically again if this any key is related to any value in the hash and then if you just do this basically like a check the tolerate something it does not work since the related value can be empty string or 0 both of which is contained also the Boolean false so we do not want to just see it like like if this kind of a thing dollar edge something if you specify this kind of a check essentially let me if something is actually has a 0 it will still term that is a Boolean false it should be Boolean false so this is what this means essentially so do not do this instead use the exist function to check whether it really exists or not an exist function if it is a 0 it still returns a true value and only like it returns a false only form such object does not exist in the hash array and then the hash variable embedded in a string is not interpolated this is another key concept over the reference to the hash element is interpolated so if you are embedding variable inside a string you cannot interpolate or hash variable that is embedded in the string cannot be interpolated so just remember that when you are missing so now we want to iterate through the hash actually there are two functions again to do this the first one is the keys operator essentially so this keys operator will iterate through all the keys the other one we will learn a little bit later then also the sort operator is another one which can also be applied to iterate through the keys in order so let us see like how we can use it so in order to get the list of all the keys in the hash we can simply put keys person this hash and this returns all the keys and we can store it into an array so the array is essentially like hash keys is a bomb 50 so this is hash is 0 this is 1 this is 2 so you can actually like in fact print out these values and then so this is to get the keys so the other one that I mentioned is using the values function again the values actually captures all this side of the equation so these are all going into here values are all going into here so what does values look like so so again it is the same thing basically so as valve 0 this one then this is 1 so here is a phone book example we started actually so every we basically have we are declaring this hash array called phone numbers initially we are initializing to a null and then we declare variable called number and here we are also like I mean have a name essentially and then we basically query the the name and the number so we start this while loop here starts here and ends here and then basically if it is not equal to end in this the mnemonic for ending the program or ending this loop so if it is not equal to end then what we say is basically like enter a name that will be added to the phone book we ask this basically and then we capture the standard in whatever we input into the name we chomp the last character which is the new line essentially and then we compare the name against end if they are equal then we skip the whole thing and then go to the beginning of the loop so it tests this condition and since it is an end it will end the loop itself but if it is not then we will go forward then we asked to enter the phone number and then capture the number in this number variable which we again we chomp the number and then we basically just add that into the hash array that we declare the phone numbers with the key as the name and number as the value this is the one that we ask for a name and then the print the number corresponding to that name so this this program is after we built this hash array in this slide so once we build the hash array now what we do so here we again initialize the name again we ask whether the name is n if it is not end then we go into the program and we basically like ask to enter the name the name we just get it from standard and we again do the chomp operation to remove the last character which is a new line here so once we have the name then we look for whether that is this in the in our phone numbers hash array if it exists then we just print out this line so look here basically this is the key and here we use the same key to check what the value is so this whole thing this prints out the value and then again if the name is we type that end at this point it just goes back to the beginning but then if it is not end and if the name is not found this exists returns of false value or a zero then it goes all the way to this else condition and then we just print out saying that the name is not found here you can do some more additional tricks and basically maybe and also say that a if it is not found do you want to add and then if the if the user says yes then you again go into just updating the hash array and writing like you can easily update the high just using this numbers $ name okay so if you add this essentially make this program complete program there if you give a name it looks into its dictionary see whether it sees whether that team has a matching number then it prints out the number if it does not have the match then it prompts you for entering a number so that it can add for that particular person or the name what the number is corresponding numbers okay there are some additional concepts in the hash essentially like I mean one of them is for the form a predefined hashes so the percentage ENV variable is defined to be a key value pair defined in the environment of the running whole process these dollar these percentage in ENV variables are inherited from the runtime environment so again in the windows on context actually like we can actually set up through the command line set command essentially like so when we do like set then we can just say like we say set then variable name equal to value in so this is essentially like I mean so is the command line set and set or even like in the Unix bond shell we can do the simple assignment similar to this we can also do like set ENV and then set ENV and then the variable value so the simple assignment will actually accept these things and then once it is done the the percentage ENV has these key value association already so then it uses that to determine how the operating system to begin so now let me give you like a small quiz essentially try to define an hash array with the following data here actually it is very simple essentially so it is basically like all of them are just strings and this side is all new it could be like I mean mix and match also and now you sort the array based on the keys try to sort the array based on the values and then print both the results and see what we get we will discuss this in the next lecture as to what you should be seeing so now we come to the the next set of topics essentially one is like I mean how do we reference the I mean what is the concept of a scalar variable and what is how is it represented in the memory as you know like I mean every variable is also has a reference to that variable in C language we call it as an address and then essentially like I mean every variable you can think of it as a variable is is a memory location in the main memory to which a value will be stored and then there is an address to it which will actually is the reference to that variable so even in curl we can actually give references to the the variables essentially essentially the reference is a scalar value given to be given the giving the address of another value in the memory so whatever the address of this particular location will be the reference and this is a scalar value the reference to existing variable is created by using the backslash operator so that is this one so in the string context we use the backslash often to escape the special characters but in variable context when we use the backslash we actually create a reference to that particular variable or calling its address of the variable and then reference to little structures and can be created so if you want to create reference to a list we can create by enclosing the list in a square bracket so this is what we are we are doing it basically by when we write a array so here $ array $ a is 1 to 3 4 means when we refer when we use the $ a that refers to this whole list which is 1 2 3 4 and then we use the curly braces essentially like I mean to denote the hash array again we saw that this is how we create the hash array essentially what we are saying is like and okay this is the hash and we create reference for it by enclosing within the calibres again lot of these concepts essentially all revolves around essentially I mean there is one way of understanding and but this is the next day to understand the same thing slightly differently so the assignment is to a scalar variable since the little value is a reference so here we do not use any of those percentage H notation or at a these are all now scalar values so are these the same like I mean are at a equal to $ a and percentage H equal to $ H only thing are they equal are they the same the answer is not because now these are scalar variables so what do they actually contain this is something that you can you can actually like do this experiment do these assignments and print $ a and $ H and let us talk about that in the next class as to next lecture as to what we will see if you do this simple print but it will be interesting to see like I mean what we get so for a scalar value itself of a scalar variable we know that actually like we can refer to that one by using the backflash what I mean is here this is a scalar like the $ X then you can say that basically $ ref equal to backslash $ X so now what is $ ref $ a and $ H or if we have these as our variables how do we get back to these arrays and hashes and the scalar variables so this process basically by which we can convert a reference back to the if the variable the original variable that is known as the D reference it okay so to access the value pointed by a reference the programmer must explicitly be the D reference the reference so if you are given only the reference we can be reference by using $ 2 signs so $ $ D in this case is same as $ a because $ B is actually backslash $ I don't know whether it makes sense but this is the way to understand that so here to in order to reference an array the arrow sign can be used between the reference and the index to indicate the D reference so we can one thing that we can do is like I mean when the $ R is at list then $ $ R will bring back the list and then if you say like number 3 then that is the index 3 of the list we can also simply use $ R and then arrow 3 in score brackets and this is the same as but if you just say like $ R this is not the same as any of this this or even this the reason is $ R start brackets 3 is actually the the third element or actually in this case is the fourth element of another array named at R and that's completely related from this one which is actually the at list so see the difference actually here it's at R and here it's at list so since these two are different essentially like I mean this is not the same as this so don't use this for this if you want to use the list third element with the reference then use this or this and if you want to refer the third element of the port element index 3 of array R then use this so I think these some of the powerful concepts in the per language in per language is simply like I mean but you want to work more with in order to understand this the concepts are fairly simple but they are very powerful and pretty much 90% of the programs themselves use these concepts extensively and I just wanted to make sure that you can get all these things number one thing is we use this hash array as a new concept of an array the key difference between a hash array and a regular array is hash array is an unordered set of key value pairs so this key value pairs can have any association and have some relationship or it can completely be different and the Perl treats either case as the same basically like I mean as if like I mean there are there is no association between any of the keys or any of values so the only association that it knows about is associated in a particular key and a particular value all the others are completely all the other bits are completely off and then we denote the hash variable with the percentage basically in the front we can initialize a hash array using just a simple notation we can also like make it empty by using on deaf or just the the parenthesis without any key value passing cell we can access by the key essentially so here we use a scalar hash with a particular key value in the curly braces in order to access that so this is different from other way like the other things that we will learn we will learn later on which I will also recap on that front in order to add an element it is very initially like we let us do a modification is basically we just modify the we just put a new value for a given key and then that just modifies the value so essentially it just replaces the that particular value with the new one and then if you want to actually add it is again very simple basically like add the new key and then with the value and then the value is that particular key value and we always use the exists function to find if particular key value pair exists in the hash array we cannot just do if that particular key value pair or hash of the key to see whether that one particularly because if the elements value is zero that particular logical test will return a false value so the exist function is the surest way to ensure that that particular key value then we can delete a particular key value pair by using the delete function and to use the delete function we have to specify the hash and the particular key and please also note that we need to specify the as this is here where I think for exist we don't need to do that and then we also saw this difference between the scalar array and the hash essentially like I mean scalar could be a number or a string the string is enclosed within the the double quotes or a single quote either one is the legal syntax and we also know that actually in the single quote only like some backslash and there is only one other specific character that will be getting evaluated all the other studies more whereas in a string that is enclosed within double quotes even it can interpolate variables within that string and then it can return the values so that those things we already saw this differences and how we can do all these things then the other data structure that we learned was array the array data structure has starts with the act sign or an absence sign and then the first element is always or the first element is always array is 0 so it is the index of 0 and 0 and then the index just progressively goes up whereas in hash actually there is nothing we can just a hash with a key is what is denoted in that we saw some examples this is the phone book we also saw the example to how to denote points on a plane essentially with x1 y1 how do we demotivize actualization how to operate on them this is like critical this is even you actually do like real estate applications like placement or routing and we want to denote from one point another point and this using Perl to write certain routines this is very useful then we also see saw how to access the the hash array access various elements so if you want to just access all the keys we just use the keys function you can also do like sort on the keys essentially or sort function is also like useful then you also saw like the values function essentially like values function gives all the values of the array here essentially like then we actually saw like I mean how the program is being written so the program has like two parts one is where we populate the hash function and then the other one is where we get the name and the display what the phone number is actually one way is essentially like we can combine these two these two sections into one where it asks for me and if the name exists it returns the value the name does not exist it ask for the phone number and then populate yeah and then progressively it can go build the entire array and that is what is shown here and actually like I mean this could be a good exercise for trying to create a phone book where it not only just gives you the number but if it is not present in its database it adds that and then finally it gives you all the entries of the phone and then we also studied about the predefined hashes essentially and then how to set them then the two main things that we talked about was referencing scalar and also dereferencing the referencing a scalar value is to reference its address in the memory so the way to do it is by using the backlash operator for the lists we simply use the square brackets and then for a hash we simply use a curly braces to create a reference and all these references we noted were created as scalars and not as those arrays or the hashes themselves so these are all like known us that we saw only like the dollar they sell out and the other one is now that once we gotten the references to these variables how do we re dereference them or get back the original array or original lists or original hash functions how do we get back to that and for that we can use the extra dollar sign essentially makes a dollar dollar being and then for an array actually we use the dash and then greater than sign which actually is an arrow sign this arrow sign is made out of hash and then greater than sign and then we saw basically like I mean if we have a list and then if dollar r is assigned to that list essentially it is the reference to that list dollar dollar r3 is one way to access the fourth element of the array and the other way is the dollar r and then the arrow to the element 3 to access the element but if we do just r3 that is incorrect because that is not the same as the previous two and dollar r3 is typically the fourth element of the array and so that pretty much concludes this lecture we will take it up in the next class from this point and meanwhile please try to do the small exercise that I have assigned to you and also think about how do we access based on just the keys and how do we access the access based on the values and how do we reverse the keys and the values so I guess thank you very much for attending this lecture we will see you in the next one thanks bye