 Hi everyone again welcome to welcome to this lecture this is again the I want to remind you about the class that we are talking about is Linux programming and scripting today we are going to continue our lecture on Perl we covered a lot of things about Perl so far we started with just doing some really simple programming Perl then we went into more kind of like we started learning about the data structure you learn a couple of them one is the main data structure that that is pretty much the workhorse of Perl is the scalars you learn about scalars and then we went into the arrays actually in between we also learned about strings we touched upon it I think like I mean there is more to strings that we will be seeing in the future classes in couple of future classes but now we will be we are talking about arrays and I want to continue our discussion on arrays and today I will be finishing up with arrays so that you have a good understanding of how to represent arrays how to use arrays in your programs and how to manipulate various data using arrays so all these things are the key things key learning objectives from this session so before I start with today's lecture I want to give you a brief recap of the what we learned in the last lecture so let us begin with that discussion in the last feature we first started talking about the range operator I hope you remember the range operator which is two dots which represents essentially a range of numbers and it has two contexts that we talked about one was array context and the other one is the scalar context and what is the difference between those two that also be understood basically in the scalar context it also has a Boolean nature so that it actually returns a true or false value so that is one thing that we talked about then we went into arrays in more detail we understood how to specify an array and how the array literal works basically what are the rules governing it in terms of putting a dollar in front and then it needs to be in a square bracket that it is specified and then array itself but the elements are specified in the regular brackets basically and then you can specify each and every element inside that array and then work on that and then they are addressed using this form the index indices or index this is in the square bracket then we also talked about the slice I just wanted to remind you about slice let us just quickly look into the slice operator so the slice is essentially it is a shorthand representation of multiple elements of an array so here this is something that we talked about basically the at sign or the ampersand sign with the name and then we can specify multiple elements form as just comma separated once and essentially like I mean that specify which are the various elements of that array and we also notice the some of the rules governing the slice basically first of all the indices need not be increasing and the indices need not be different basically the same indices also constitutes the slice then we looked at the stack essentially stack as I mentioned basically it is a set of memory basically which are which is stackable you can say like meaning it has a property of lasting first out so that is what this LISO means basically lost in first out which means that essentially the elements that are added first comes out the last and the last one that is added into the stack comes out the first and the operation for putting an element in a stack and then removing the element in a stack that is the two things one is pushing the putting the element inside the stack is known as push operator and then when you are removing the element from the stack we use a pop operator to never move and here you see one key thing key difference between this and then regular arrays you do not specify any kind of index so you just say push at list and then that is assigned to a new element and then so basically it knows that whatever is last that you added that element is actually like I mean so that new element is added as the the top of the essential and it automatically increments the index but the index is unknown to the user so the user only sees the only one element of the stack and then here is the pop usage of the pop there one basically where we are assigning the pop at list to the this variable dollar top and again what this does is you do not need to worry about okay whether it is index hundred in this 150 whatever it is but the top of the element of the last added element is just pop and then it is put inside this new variable dollar top so this is something that we learned and then we saw some examples of this and more more more things basically that we saw then we looked at this shift and unshift again these are some of the key elements essentially which is kind of this one is like opposite of push and pop very similar but it is kind of opposite in the sense that the shift is essentially like to the added the element is added to the lower end of the array instead of the top of the array so the last element is in is incremented last index is implemented and then it is added to that so here shift means basically like the last one that is added that gets popped out basically and then it goes into the array and then the array is kept as is essentially like and then unshift essentially like I mean it puts back that at the top so these are some of the useful commands essentially like I mean so when we say like unshift 4 5 actually like I mean you can see that basically 99 is pushed and then the 4 and 5 are added at the end and then we also saw some examples of the shift command here is some of the some more essentially you are putting the thing inside that and then we are also like shifting there the $x will get last as the value and then essentially the remaining array will be intact so that is the recap from the last lecture now we are going into the today's lecture we will continue with the arrays I want to introduce a couple of new things to you guys basically so let us see the first one is the reverse operator reverse is essentially used to reverse a complete an array with all the indexes or indices for complete the worst so so in this array for example so the $a is 0 1 2 3 so this one you can say like it is the $a 0 element this is $a 1 etc and this one is like $a 3 so now we say the new array at B is reverse of at a one thing you notice is essentially like when you declare a new array you do not have to declare it up front you can directly make that assignment in the modern Perl actually use my as a moniker for specifying a variable it is it is a good practice to actually even if you are directly using it to specify the array with them the my in in front so this is this becomes like my and then at B equal to so this is a good practice whenever you are using the at B for the first time use the my then Perl knows that actually needs to allocate the memory for this variable so now let us look at this essentially so when we say reverse so we are assigning this as a reverse so now for B 0 that value is 3 and B 1 is 2 and B 3 is actually 0 all all have a $ in front so in this example we are printing the a corresponding a from 0 to 3 you can see that it is actually like 0 to 3 the values themselves whereas for B the B 0 corresponds to 3 and then B 3 corresponds to 0 so we basically like I mean the way to print out this is essentially like I mean specify like I hope I mean you remember all these things already so when we escape this character essentially we just print directly like at B and then here this is literal because it is all inside the code so it won't do this operation it just prints out the same value but then we when it sees this portion this it converts it into its array and that is 0 1 2 3 so basically like I mean this is also like the way that you can write out this sentence okay and then the other key function is the sort function this is another one that you will be using more often in in this in a real world so here an array is given 7 1 9 3 and then we say let's take okay at B is sort at a so now if we print out that B it prints out 1 3 7 9 so you can see that actually it's sorted the the a in an ascending way so this is an ascending sort and then it prints out 1 2 3 1 3 7 9 and then the a is actually like I mean you can see that actually it's kept the same thing basically so so the question for you is how do you do a descending order sort think about it what can we do for a descending order so one quick solution could be that we can just use this first start the a and then basically like reverse it essentially or you can say like that be equal to reverse sort so this basically like I mean you thought that a first and then put it into what happens if you just do the reverse first and then thought it will actually give you the same result right because whatever you are reversing actually doesn't matter and then basically like once you start still the same so now what about this array essentially and then what happens sort C so can you tell me like I mean what happens again this is you should think about this is also like an ascending order but now it is on the the alphabets so we know that actually like O comes before T and then in this one also like T H comes before TW and then finally this comes so this is one this is two this is three this is four or in full notation this is zero this is one this is two and this is three so let's see what happens so here when we do the sort one see that this becomes zero one two and three so now we have another one which is like multiple numbers all have different number of numbers essentially this is just seven this is 101 29 300 and if we do a sort what happens essentially like in the sort does not go by the actual value of the number but it's only a literal sort so in this one this is the first one and this is the first number this is the first number and this is the first number so it first starts all the first number so it comes up with 101 or essentially it's 129 thousand and then 3000 and then seven so the final answer is actually you can see that basically it only did the first number sort the remaining ones it leaves open so to achieve that the real sort what here what we need to do is we need to depend with zeros for example so make everything like four digit number so basically 0 0 0 7 and then 0 2 9 0 actually 0 0 2 9 0 1 0 1 and then 3000 if you do this number then this order will be maintained and then this is what we can get as a result okay so I think this this is an interesting assignment or is an interesting example as to how to do the sorting so now let's look at some more functions essentially like chopping arrays so we can apply many operators to an array the operator is applied to all the elements of the array so in this one like I mean so chopping is one of the operations that when we apply this particular operator is applied to all the elements of the area so the net result of this like I mean so if you do a chop on this particular array can anyone tell me like I mean what will be the result so essentially like the this element this new line gets new line goes away from this element again the new line goes away what about this one so let's see so again remember chop removes the the last character so from here this exclamation mark is removed so we only get by so if you do like a chop all the stuff this is what we'll get we'll get the hello we'll get the world and then we'll get the back you won't get the new lines from the first two and then the exclamation character from the next one so again as I mentioned like I mean the earlier the various operators have these various context simply the scalar on the array context so the it takes both the scalar as well as arrays as operands it can return scalar and arrays of output if we want an expression to be evaluated in a scalar context we can force that by using a null string to it so so what do we what do we mean by that let's look at some of these examples so here is an array array is given as X Y and Z and then we print out these elements basically it just prints out the same thing basically X Y and Z now we say essentially like okay how many elements are there so again this prints out and we include the the comma operator basically like it just you know concatenates everything and then prints out now there is the the next one which is we are concatenating comma to the array now what happens so here essentially it only prints the number of elements which is now it's no longer an array context so this is all like array context now this in this example this will become a scalar context again we go back to the the original rule so in for the arrays essentially like I mean again like I mean we can have the scalar context are the array context and the operators essentially like I mean so they can force we can make them evaluate as whether scalar context or an array context by just concatenating and null string to the array so in this one we saw that actually once we give a null string basically in this case like I mean a comma which is now treated as an operator and we are concatenating that to the this one to this array and there now it is evaluated as a scalar and actually gives the number of elements in the array as opposed to the literally printing all the elements so here we printed out all the elements whereas here it prints out just those the number of elements we also saw like an operator to print out the the number of elements in an array I hope you remember that just going back to give you that information this was covered in the last lecture when we talked about actually it says even before all these things yeah right here length of an array so this is another way to get the array link essentially which is the $ hash array name basically or here we also mentioned that the dollar size is dollar array so so when we specify the dollar actually like I mean that changes this the context into a scalar context and then basically like I mean we get the length of the array so I hope you remember that so that's how we are printing that and here we actually like printed out that with this is a essentially like pretty much what we did at this point which is we are concatenating with a null string so see here and then that prints out the actual size so think of this like I mean we actually did this in the last class and now essentially like we are just formalizing how we did it by describing this context essentially so let's go back so now let's look up look at the some of the things that we have been learning earlier which is STDIN as you know like I mean that is the input to the array from the terminal so far we have been learning that as a scalar context where we actually use it to input one value at a time now let's see like I mean how we can use it to use it in a array context and then see how can we use it in that in an array fashion so in the scalar context it returns the next line this we saw actually we are in the when we studied about the scalars whereas in an array context actually it returns the rest of the lines so in an interactive mode we need to use the control D that says the end of the funny in the file character for Perl so how do we use it is using these two notation so when you do a $ a equal to standard in this is a scalar context and if you use an ampersand a standard in this becomes an array context so as you know that essentially like I mean basically like what you are finding to is how it determines whether it's a scalar context or is it a it's a array context so now let's look at some examples also of how we can use it and before that we will let's look at how what is the array variable interpolation essentially so so first of all in the print we already saw that actually like I mean even within the double quotes arrays can be interpolated and it can print out the values so here when you specify like I mean this is like you can think of you can see that actually it's not a just a one either a numerical array or as a particular it's it's a combination of both and it it Perl actually cannot distinguish between those I mean it treats them as the same so it's very versatile in that sense so that's why like I mean print out ABC and 1 2 3 when we use the slice essentially like I mean for here it's 2 3 so you know that this is index 0 1 2 3 0 5 so now what it prints out is CN1 which is the correct thing about index 2 and 3 so now when we do this is much more trickier now like I mean we do the slice of at a and then these 3 4 5 so now what what should it print out so 3 4 5 we know that it's actually like it's 1 2 and 3 so when we do another ad basically like I mean that is needs to print out this 1 2 and 3 which is DC and 1 so let's see what it prints out so that's the it prints out DC1 so again I want to reiterate this one so 3 4 5 so first it's basically like at a the 3 4 5 evaluates to 1 2 3 and then it prints out what is 1 2 3 which is DC1 so you can see that actually like I mean in this case actually it is it's actually interpolating it twice basically like I mean it's first interpolating first set of values and then it goes into the second set of values and in this case like I mean if you make a mistake and say like 1 2 3 or something like that first one as you know like I mean then we just print out nothing because those ABC are not of real values and yes you know like slice function we cannot have non-numeric values so now let's see like how the array references are done in quotes so in this case like again within the quotes this is actually interpolated so a1 is again I will mark it mark these ones as 0 1 and 2 so you can see that actually in this case it will print out the y because one corresponds to this one so one corresponds to y so the y will get printed out now let's see like I mean in another case x is equal to $ x we are depending that 3 and then $ x-1 so what do you think this should print out so in this case again this is first evaluated and then it goes and interpolates for the array value so in this case it should print out the V which is what it does now when we do this one alone what does it print any answer it directly prints $ 3-3 because see now it knows that actually it is a index context but you know there is nothing there here so it will print out directly this so now we have another question if you want to print out this like say in kind of expression what do we do so one thing to note is essentially any this square brackets after a $ variable is always considered as an array reference so if we put like $ a $ x-3 it cannot understand that okay if you want to print out literally like I mean this way if you cannot print out because if it doesn't know that okay this needs to be printed out this way so in this case it returns just x which is not what we want we want to see like a $ x or 3-3 something like this so to prevent that from printing just x and actually like stopping the interpolation of the array variable itself we have to use either this or a concatenation or backslash like I mean all three are okay basically to do this one so so what do we mean essentially again so in this you think the this the the the brackets essentially so you can specify saying that $ bracket a and then use the car the the square brackets and this is one way to actually avoid the and print literal terms basically so in this case like we can do this way and then this is evaluated as one separate entity and that is not confused with $ a this kind of an evaluation so this is one way to do it the other way to do it will be like $ a and then you concatenate with it $ $ x-3 so the concatenation operation again basically like avoids that confusion and make sure that we have the we will get the desired effect and then third way is basically to use the backslash is like $ a and then back escape the square bracket $ x-3 so all three are can be used otherwise it is just treated as go the variable so let's look at some examples here x is an array and y is $ 3 so if you do a $ x and then square bracket $ 3 this is actually like wrong because there is it's the single element array so again you know that basically a string is an array of characters but then you specify like this it's a single element one and also like I mean in this one this variable is treated as a scalar variable so there is no array context to it so it becomes wrong so to make sure that we can print this way what we need to do is essentially like I mean to use the packets so in this case it will print as array element 3 so again this printing as element 3 even though this may not exist other way to do it is little x concatenated with the all an index so that's also prints out as the same array number three and then the third one that we saw was to escape the various characters so we can use $ x escape the square brackets and then $ y one and then this one this will print array two or array literally like $ 3-1 because it only evaluates this won't evaluate this function because there is these square brackets are so it just puts this and these times at the same time if there was such an array as $ x equals one two and three four whatever so in this kind of case basically if you don't escape this these miss these ones then it will just print out it will first evaluate this 3-1 which is 2 so this is 0 0 1 2 and so it goes and prints the flag just prints 3 so keep in mind this one this will help you with a lot of bugs avoiding a lot of bugs essentially so I think this is one of the key things that we will learn in this class also like I mean there are some some other types of arrays that we will be learning basically in your courses so I'm not going to go into like a lot of details at this point I think like I mean right now like we have a lot more knowledge than what we started with which we can actually like now really go into more challenging doing some challenging work so here is one quick quiz for you so at a is defined as the four elements array what is $ a if you specify $ a equal to add a what does that mean and if you specify parenthesis $ a equals at a what that means so I'm sure that you may have guessed it right away that this is something that we covered in the last lecture so at a denotes the number of elements basically sorry the $ a equal to at a this assignment it assigns the number of elements to that this one so are converting the array context into a scalar context so the answer is $ is 4 but what about this the second essentially here it's actually like now it combines that so what does that mean basically so when we specify this one this actually means the first element of this array is assigned to $ a so this is something that we use not a lot of times basically let's go it is so if you specify like I mean you can I say that this is there is one two three four and then basically the first element is captured in $ a and then the remaining is like and then that's this two three four and we will see like I mean what are the some of the so call actually like assigns these numbers these variables automatically so in the next lecture or I mean in the future lectures we will see like I mean how do we get this remain because this is something that is very very commonly used operation even like I mean we can say you know we will say basically like I mean read any line and then split on something and then we can say like okay assign it to ABC in this context basically what this means is basically like I mean the first element the 0th element is assigned to a and then the 1 is assigned to B and then 2 is assigned to C and in this case like I mean if 2 does not is not there and whatever the rest is assigned to C so this is an useful operation so try to remember this we will be also like learning more about this and then so this is pretty much like end of this one dimensional array lecture so I hope like I mean this is useful and right now we talked about just a single dimensional arrays we will go into more complex data structures which are the multi-dimensional arrays essentially multi-dimensional arrays also we can use lists and lists of lists to represent these multi-dimensional arrays we will learn about that again static assignment will still get like lot more memory intensive in nature so we will typically avoid that but essentially like I mean we will see how lists are done in Perl in the next class so I think that's pretty much done so let me just do a recap of what we learned today again we started with some other new elements in the array with this basically like reverse function so reverse function is essentially like I mean it's an operator on an array and we get the value as essentially the indexes being reverse in a reverse order so if you have a 10 element array when we do a reverse operation it still returns a 10 element array but the indexes are exactly opposite so the 0 element will become the 9th element and then the 1st element will be in the 8th element and so and so all the way and that's the reverse operator then we also looked at the sort essentially like sort is an ascending order sort and it does not it's not a numerical thought per se it's really just an alphabetical sort but we can use it as a numerical sort give it sufficient number of numbers or make all the numbers same length then we can make it as a new sort and we saw actually like I mean if you do the 7193 then it will turn proper way and then we also looked at how to do and descending order sort which is like 9731 how to get that those numbers and for that we use both the functions reverse and the thought and then it can be evaluated together and we'll get the required or required effect and then the even any string arrays can be sorted and since the sort function is based on the alphabet so the string arrays will also be sorted in the logical order as in this case basically it will be 1 3 2 0 that will be the final number so when you sort like length with multiple length numbers you have to make sure you have to take care because it will still sort based on the first number so that you won't get the desired effect then we looked at the chopping the array essentially like chop is when we use chop as an operator those of that operator is applied to all the elements of the array so here when you do the chop of add stuff every element is getting chopped with the last the last character so or hello and world it's just a new line those gets thrown away as in by the exclamation mark also then we formalize the concept of the scalar and the array context the first one basically we saw as to how to represent the array itself in a scalar and an array context so in an array context essentially like I mean we just let it be and then we get the answer as just XYZ if the arrays elements are those elements whereas if we concatenate the array with null character then we will get the number of array of the scalar context which is the number of elements inside that array and so we get like I mean the actual number itself then we also looked at the STDIN as an array because again every variable will have this scalar context and the array context so if you specify like $ a equal to standard in then that assumes that it's a scalar context and returns the next line whereas if you do an at a equal to standard IM then it keeps getting line after line until we hit control so that is then in the array context then we looked at how arrays will be interpolated in various situations mainly it to print arrays in in ports essentially like I mean we need to specify the codes and then we specify the array inside the code that array will be evaluated and then for slices the same thing basically slices will be evaluated the we interpolated within the ports and then it brings out the answer appropriately like also can have like the multiple evaluations essentially you can evaluate it once and then further evaluations will be similar kind of things basically so you can have so this is this is something like I mean to keep in mind because we will learn about these kind of things in the multi-dimensional context as well so in this case essentially like the at 3 4 5 as a slice results in the values 1 2 and 3 which again is a slice and then those values are the time which is B and 1 so that is another thing that we learned in the in the context related or how to interplay and then now let us look at that the other interpolation essentially this is another thing that we learn so when we try to interpolate arrays inside the codes essentially like I mean so it is evaluated and then basically completely evaluated and then we get the result so for example in this one got y as the result and in this one we got the V as the result and then the other thing that we also noticed was when we do this kind of thing just just the square brackets without any average presentation in front it literally prints out everything so it prints out the square brackets it prints out $x which is actually 3 and then it also prints out this and this minus sign so 3-3 so the center thing is getting printed out so that leads to an interesting conversation basically once we have the the array reference itself then how do we how do we use that or how do we distinguish between an array reference and a non array variable so there are three ways to distinguish that is described here one two and three the first one is to use this the brackets essentially so that is what we mentioned basically like a $a is a scalar variable we use $ in brackets a to distinguish it between a scalar variable and an array with an index and then use like $x 3 so and then the second method is using the concatenation so we instead of saying that $a and then like this we use the concatenation with the the next one so here essentially like I mean this is treated as one little variable and this is another variable so whatever this variable may be evaluated this is just represented as this is like this one and then $x evaluated which is 3 so it kept like and then finally the using the backslash or an escape character this is this one basically like which is used to escape various control values essentially for example here the square brackets can be used in other contexts essentially for the representing the array index but once we escape it then a literal value is used which is just the the square brackets so using that we can actually get around mistaking the $ variable for a context so this is something that we also want and here there is an example that we talked about one thing to note here is when we have the $x as a scalar context which is from here if you use it in an array context Perl does not like it and it will error out so here it explains all the three methods that we talked about essentially like how to print the arrays and then finally like we also saw like a small quiz to distinguish between a scalar context and an array context for example in this one it's a scalar context this is the example that we saw earlier so this prints out the number of elements of this array which is the same as 4 and then the other thing that is of relevance is so this using the parentheses to to actually get values from $a or at a which is the array essentially so from here when we assign that to a $a what we get is essentially the first element from the at a which is element one so the first one the answer is 4 and for the second one the answer is $a so I think like that's pretty much it for today once again thanks for listening and thanks for all the all the things okay I'll see you next time thanks bye