 Okay, again now welcome to the next lecture of the Linux programming and scripting today we are going to continue our discussion on Perl we have seen some introduction to Perl we introduced some data types we went into an in-depth analysis of the scalar data type and a few control structures that we talked about so let us just do a recap of what we discussed last time we also talked about some operators we already covered the operator presidents I hope you remember the presence table because that will be used in all the lectures going forward so just keep in mind anyway let us talk about that so in the last lecture we covered the auto increment and the auto decrement commands which are these plus signs the double plus signs and we also like saw some differences between to put the plus first or towards the end we also saw the auto decrement which is actually the negative commands and again we saw like how they can be perl and how various things will be interpreted differently we also talked about the chop and the chomp commands the chomp command is the command that you this used to remove the newline character from the words essentially we saw that and then we took several examples as to how to use the chomp function which is pretty much like I mean used everywhere today like I mean also make you read the command read any line from a file you want to chomp and get rid of the newline characters again then we talked about some of the scalar interpolation basically like once you have a scalar variable how it is calculated within a command within a print statement inside perl we will we will talk about talk more details about this form in the next slide we also talked about the STDIN and what how that functions you briefly looked at this $ slash as a delimiter basically and then how we can use it to read multiple lines from a file or multiple lines from a standard in input and then finally we also saw this under form essentially so I am going to actually take some more details about these three like I mean just what we talked about last time in case the last time what we say what we bring the same so let us look at the interpolation of the scalar variables mainly we talked about the rules governing this the variable interpolation occurs inside a double coated string essentially so this is one of the differences that we saw in the previous lecture where if the singer the difference between a single code and the public code and if the variable has not been assigned the value then the variable is replaced with an empty string so that is another key differentiation essentially or key rate to distinguish so the program will not generate an error instead it will basically it is the unassigned value with an empty string one other thing is like there is no double substitution meaning cannot substitute once and then have that as an another variable so that you can substitute it again so it is only one time use essentially so that you can understand the thing then in case like I mean you are you have letters basically that are continuous and then part of it denote a variable then you can use these the brackets essentially to separate the variable from the surrounding text this will be more apparent when I talk about talk about this next rule which is right here which is the longest possible name will be used as a variable in a string so if you say like okay my name on a higher and then you put a dollar this whole thing is treated as just one way to do this point say like I mean you want to actually variable is so dollar Anand and then there is higher is just some set of text basically to denote this you may want to put the parenthesis here so that it knows that okay dollar Anand this one variable and higher is all the surrounding text so so you can now see like I mean the how this rule will be applied and why these two rules rules go hand in hand so the other thing that we talked about was the STDIN every time we you we use a standard in in place of a scale of expected the pearl reads the next line from standard in so the essentially we can ask it to read this any any amount of text from the terminal by just using the standard in one thing to note here is the line read includes the new line character so if you want to remove the new line character you will do a standard in and then you need to do a job to get rid of the new line character and then the default STD IN even though like I mean you can see that this is actually a file handle this is a terminal essentially so you read in from the terminal and then you can do things with it and then as I mentioned basically almost always job the new line of after we read in the STDIN so this is the key things about the STDIN we also saw some examples I am not going to code the examples this is still talking about the recap and then finally the undef value that we saw essentially the undef is actually you can think of it as a value and that value is used value is used before it is given a value so before you give it a value value is what is the undef essentially it is not equal to 0 or it is not equal to a null string so I think like I mean that is one thing that you want to understand only like for the number of reasons the undef is treated as 0 so if you have any kind of a dollar a plus dollar b where the dollar a is not defined then this will equal to 0 or this operation essentially so otherwise like I mean it is like not the 0 but when you perform an operation with that value with that variable then it is treated as 0 and it is also treated as an empty string for string operations again these this rule does not hold good when you perform an operation and then a city IN returns undef there are there is no more data so now we will start today's topic I think like I mean so this is fairly clear we will encounter this under in many programs in the coming lectures I just wanted to again understand and keep in mind how to use that so today we will start our topic with the discussions on array and list data mostly we will be focusing on array today and lists are basically we can talk about it in the next one again what is an array array is an ordered list of a scalar data in for one good thing is like there is no declaration needed to define an array and the size can be 0 to any number mostly like this number is limited by the amount of memory that is that is allowed in the machine and we can easily increase the size of an array by just adding new elements to it so we will see like how we can define the array and how we will do addition of arrays so the array literal is a list of comma separated values imposed in parenthesis so we will see like how to become a real literal and then there are some special operator one is this one this is there called the range operator it has two meanings depending on the context and we will see that so let us first look at some of the examples of memory so this is an array literal basically the two parenthesis and then the values are enclosed even the parenthesis and they are comma separated this is another array which has a string as one of the element and then there is a number of another element this is another array if you define this basically then the dollar a becomes the first element and 99 becomes the second element otherwise this becomes an undef or 0 in any operations it performed this one essentially like I mean it is an automatic operator so with an array and then this is an empty array of array of size 0 and then we have the range operators 1 through 9 so actually here it will be like 1 2 3 4 5 6 7 8 9 that is your array and in this one it is 1 2 3 4 5 10 and 20 so now what will this array be essentially so again here it is we will talk about the rules as to how to do how this array is defined but essentially like I mean it will be like this 1 2 3 4 5 and even for this one it is 1 2 3 4 5 because it only takes the integer from the first and increments one at a time until it reaches the final number so these will be known this will be more so let us look at the two contexts that I mentioned for the range operator so the first one is the list context which is pretty much what is defined in here all these four examples define the list context of the array in the list context it returns the list of values counting up by once from the left value to the right so the left value is greater than the right value then it returns an empty list so that is another key thing that do not so but in the modern implementation so like I mean if you look at some of the Perl book the older Perl books you will see like this one caveat written in that where if you use this kind of a number one dot dot one underscore 10000 10000 then it can burn a lot of memory because it creates an array for your temporary array for you but this temporary array is no longer created in the newer implementation of Perl so this statement even though it is not a very good use it's it's it's a it's a safe statement it's not a it won't cause any issues in the statement the second context that I meant to mention is the scalar context in in scalar context the operator dot dot actually presents a boolean value so it is like a flip-flop it emulates the long-range operator of said arc and other editors this is the line-range operator is also known as the comma operator so each of the dot-dot operator maintains its own boolean states even across calls to subroutine that contains it so the way it works is it is false as long as the left operand is false once the left operand becomes true then the range operators stays true until the right operand is true after which the range operator becomes false again it doesn't become false till the next time the range operator is evaluated so you can test the right operator operand and become false on the same evaluation and it becomes true but it still retains true at least once so these are all like I mean big set of rules let's see like I mean an example because that pretty much gives us how this command works so here is a simple scalar context essentially where we use this number here 101.200 print so the way to interpret this command is essentially it prints the 200 to the second hundred lines essentially so basically it prints from line number 101 to 200 but it is the short for these conditions basically like dollar dot equal to equal to 101 and dollar dot equal to equal to 200 print so the question is like I mean this is another dollar variable we saw like two of them before and now this is the third one which is the dollar dot operator so the dollar dot essentially it's the it's the short hand for input line number essentially like I mean what we're saying here is this 101 is actually the line numbers so the line number is equal to 101 all the way to line number 200 print so here actually like I mean this corresponds to the quality operator but there are other ways of using the dot operator as well so in the scalar context and so you can test multiple conditions and then come up with these answers for that so now let's talk about the array variables so we saw like the scalar variables actually they start with the dollar the array variables are they start with the ampersand or at so since they have different prefix compared to a scalar variable you can have the same name but you can you can treat them as two different variables for example the ampersand a and dollar a are actually like two different variables so you can use them interchangeably or you can you can I mean you can each will have its own value so it's not dependent on one value of the other okay so that is one thing and the array assignment essentially where arrays can be assigned by using the assignment operator the value of the assignment is the entire array so how do we use this so here are some examples first of all declaration of an array we know that this is the way to declare an array and then we assign a variable which is the ampersand a and here this is then string array containing colors the names of colors so at color is red white and blue and at be equal to one this is also interesting array containing this one element here is another one dollar C is basically 0 at a and 9 so this can be you can replace this here so that it becomes like 0 1 2 3 9 and then the same thing the gray is actually updated towards the end to the app called a very and you can see that actually means used both places and this is a multi assignment case where actually like both dollar I mean at E and at B are assigned same as color so here so one more clarification here which is the one I mentioned basically is a single element array so it's basically it will be promoted to like the parenthesis so that it can be the it will be an array type so here the same thing like the 0 1 2 3 9 and then here again the grace added to the end and then this one is so both the dollar D and dollar E are assigned the same color as the variable so the array literal essentially like we saw this briefly essentially like I mean the little contains only the only variable references does not have any expressions or constants the array literal can be just used as a variable it can be used on the left-hand side of the assignment so let's see some array literal so here dollar a dollar B dollar C equals 1 2 3 so what this means is form essentially like the dollar A gets 1 B gets 2 and C so let's define here and then the second command is essentially like dollar A dollar B equal to dollar B dollar A even though you can see that actually they are the same this is what is called a swap so we just swap the elements so now the color A becomes 2 and dollar B becomes 1 so now let's look at that the first dollar first and the dollar rest is dollar A dollar B dollar C and so the first is whatever be the first and that gets assigned to the dollar first so the first is in this array it is a 1 so it gets 1 and then the rest is dollar B dollar C and 99 which is 2 3 and 99 so here again like I mean we are not doing the swap this is before the swap so that is why like I mean it says this values are 1 2 3 and 99 if it is applied after this statement then this will be 2 will be assigned to first and then 1 3 99 we applied to the second all the rest now you can also have an array followed by a scalar variable inside the assignment so in this case like a dollar all and dollar empty essentially the dollar all contains the dollar the ampersand rest so or sorry the ampersand all contain all of the elements from ampersand rest which is in this case it will be like 2 3 and 99 and then the empty is essentially like I mean it returns this under so it has nothing in it and it just returns under past the next level so I hope like again this these examples illustrate how the array structure array little can be used so this will begin using many many programs essentially used to collect information and then also like disseminate that information to the other sections of the program so now another question is how do you estimate or how do you determine the length of an array the the length of an array is determined by a scalar variable and if the array variable is assigned to a scalar variable the number assigned is the length of that array so what does that mean so you can just say basically okay dollar a equal to the all or at a so in this statement essentially like I mean whatever the number of elements in at a is assigned to dollar a actually to just slightly bit more complicated than that essentially so this one now it takes the number the thing so there is also a special variable which is dollar hash array this is essentially like stores the upper bound of the index of in the array what that means is the bound the index of the last element of the array is stored in this variable so for example in this case it will be like the dollar hash a will hold the the index of the the last element so typically the array goes from 0 to the the index of last element the index so usually we want to add 1 to the last index to get the number of arrays because the it starts with 0 so so that is the reason why like I mean the array the whole array to the dollar side the value of size is actually dollar hash array plus one so this one denotes this one that is needed to represent the 0th element and then this will be the big index of the last element in that array so some examples here so here basically the % a is 1 2 and 3 so now when we print the just the whole array basically prints 1 2 3 then prints this square brackets also because they are marked as literals here now we want to print the size and can you take a guess so the size is actually 3 that is what we got and then the next one we are doing essentially we are just adding additional one more element which is last to the array and then we are printing out the that essentially with a square bracket beginning and the end so now we get this and then finally we have this size equal to % a which it should now denote size as 4 because you added one more element so now the element array access itself we kind of figured out that we know we can use it with the subscript operator which is a square bracket and then use the dollar or the substance variable the array subscripts typically starts at 0 we already saw that in this slide that when we ask for sizes 3 but essentially it denoted as this 1 2 and 3 so essentially like I mean it is that dollar pound or dollar hash array plus 1 so and if an array index is outside the boundary the value is so this is one of the key concepts of why we want to understand what understands for in the very beginning now there are some examples so the ampersand color is red white and blue and dollar X is dollar color 0 so you can say what this has and now the dollar color 2 is changed to yellow so now and then here there are some more some more ways of accessing the array so here actually like I mean so one thing to notice like I mean so this color 0 means like so dollar X is equal to red actually yeah and then here color 2 is yellow that means that color 2 is the last element of the array which is blue and then that is replaced by yellow and here again like so this is more like a string array or string array now let us look at these examples here the array is declared as a numerical array which is 0 1 2 3 and then we increment the third element by 1 so that means that the 3 is becoming 4 and then we write out so here we are doing like a 1 a 2 is same as a 2 a 3 that means that we are swapping it so we actually have the array as 1 0 sorry 0 1 2 4 and then now we are swapping the first in the second with the second and the third so this means this will become like 1 1 or 1 2 2 4 and then if we print dollar a ampersand a minus 1 then it prints number 4 which is the last thing that is because dollar a denotes the current value of the current array essentially and then the current array line number and then ampersand a minus ampersand a itself is the number of elements of that array which is 4 and then the minus 1 out of that so this becomes dollar a 3 which is essentially it goes and finds out the last element and then prints out so that will be 4 so now we introduce another concept called a slice a slice is a shorthand representation to denote multiple elements of an array so in this small example ampersand a 1 3 and 5 is essentially to the same of dollar a 1 dollar a 3 and dollar a 5 the indices of the the slice does not have to be increasing for example ampersand a 1 0 5 is perfectly a legal one and also it need not be different it can be like this 1 1 1 which is essentially says that okay the first element of all the my file I want or my array I need the same value to be set three times so if you look at your essentially let us say 0 so it will come in so this is the 0 element so it will start printing just 1 1 1 okay so here are some more examples essentially so dollar a is 0 1 2 3 4 5 that is here we go actual array and then we replace 1 3 and 5 which is 1 3 and 5 we assign the 2 I mean actually like sorry 1 so 1 3 and 5 will be replaced by 0 2 and 4 so essentially like I mean so it becomes 0 0 2 2 4 4 as the as the main values of this array and then when you print out 4 2 and 0 that is those there 0 1 2 3 4 and so forth 0 1 2 and then it's been so 2 4 2 0 that's what we noted here and then finally we ask it to print $ a 3 and at it so then you see the difference essentially and then you can see how that will be printed out in this case both of them are the same for and so 2 and 2 $ a 3 is the same as here and dollar I mean ampersand each is also the same so one other key thing to note about the slices and array itself can be used as an index of the slides so here example is like $ name and it has the index of $ even which is also another array so here is another example $ a is defined as 0 1 2 3 4 5 and B is actually like just one shift to the left which is 1 2 3 4 5 and then then is 0 so when we print the $ ampersand a we get 0 1 2 3 4 5 and then we say print a and then in parenthesis probably so what will you get pretty much the same essentially like so it could be used the BS the next so you get the same results now what is like you use B once again and then try to print that essentially so now it's almost as if like I mean it's a shifter so shifting to the left so or essentially to the right so the the two actually like goes I mean actually the one goes to the toward the end and then you get 2 3 4 5 0 1 and then similarly like I mean you can actually increase the form on the side B and you can make it more and more a shift register so now the next topic is how to build array as a stack essentially so a stack is you can think of like a like a jar of say biscuits essentially so the elements that you insert first goes to all the way to the bottom and then basically like when you are popping essentially like I mean it only pops from the top so all the elements in order to access the last element of the first element that you pushed in you have to element all you have to access all the elements before it pop out all of them and then only you can take the the last one this particular kind of arrangement is also known as li fo I think this is quite familiar to many of you it is lost in first out so how do we implement this kind of complex data structure so one thing to notice when we talk about stacks the stack is still aligned at the lower end of the array as the next zero this is one keeping why we can easily access essentially like I mean and then if it is not aligned then the data can get copied so what we do is essentially like push a list with the new element so at least is the the variable that holds all the value so so this is one way to do it is at least and then new element use the push command this appends the element at the end of the array so you have like one two say five and then even your turn comes basically that gets added or six so somebody has to pretty much like do memory management all the time to understand what is going on the other main thing is the pop essentially the pop function functionality is shown here which is basically pop the list and then obtain the top one to the dollar top so you can think of it like this basically so when you this is for push this is 0 1 2 and then vice and continues on for the pop you have this array 0 1 2 3 and then essentially the way to pop the thing is basically final variable and then this variable holds elements from the top so so first you need to clear the third element before you can do the second element before you can do the first one so I think like I mean that is pretty much this topic now shall we go into the so before we go into it essentially let us do some examples again so here is an example where the array is defined as 0 1 2 3 4 5 and then we have a new scalar dollar new it's equal to 99 and then we say basically push this dollar new into the array so what that means is essentially like this push towards the end so 0 1 2 3 4 5 with 99 at the end now if you push two way letters 6 and 9 6 and 7 so now already distanced to 99 so when we push 6 and 7 it became 6 and 7 now let us look at the continue with the same example and into the pop section we assign dollar top to pop ampersand a so when we try to print ampersand a gives like 0 1 2 3 4 5 99 and then 6 and then we define another array variable called the dollar B then it is assigned to 1 2 and 3 and then we do a push dollar B I mean ampersand B to ampersand B that means that the concatenate this at least two times so we get 1 2 3 1 2 3 now if we push another array 7 8 9 that array will be like all these constructs are lost then there is basically it has a long array longer array now 1 2 3 1 2 3 and 7 so let us see some examples so so here we have a declaration where ampersand a is 0 1 2 3 and then dollar a we defined it as for 99 and then essentially like I mean so it the result of this statement is 0 1 2 undis and then 99 so now the dollar top is defined as we want to pop pop stuff from ampersand a and then so that was successfully done and then we say basically you can print the pop to the top and that prints out this information 1 2 0 1 2 and then undefined and then top equal to pop and then pop stop this will print out 0 1 and 2 okay so you will also like go to the lecture for shift and unshift so the shift and unshift is very similar to the pop up we say basically the this is the beginning of the array basically so this is the beginning this the end essentially like I mean now you have a partition here and you basically like memory they are going away essentially like on and then contractors so in this one essentially the so the way this will work will be basically the each element is pushed into the array so whereas in a shift actually so you can think of this as the so the push operation essentially like I mean pushes the existing elements down and pushes at the front so this is the beginning and this is the end so everything is pushed to the end so and then the pop is back the same way whereas the shift operation essentially it it is on the lower end of the array essentially like I mean so the index of that will be the new one essentially we will see some examples here so the array is defined as 99 0 1 2 3 and then we shift the array and then the result is essentially like 0 1 2 3 because now this 99 essentially goes into the beam essentially so the B becomes 99 and then the array stays the array has 0 1 2 3 and then when we say like unshift that particular element then it is back basically like it is pushed into the array and brought back and then we see say like unshift 4 and 5 so those 4 and 5 are added at the beginning of the array so it is 4 5 9 9 9 0 1 2 3 some more examples on the shift so if you say like unshift dollar tag the tag or the tag is defined as last and then that gets added as the first element from the array and then if you do an X dollar X equal to shift and percent a then the last gets assigned to the shift assigned to the X and then the array becomes just 4 5 9 9 0 1 2 3 so I think I am going to stop at this point we will talk about the rivers sort and those type of functions in the next course and again we are we are still talking about the arrays themselves basically so arrays are which data structure and it is used everywhere and in everyday life but we have like very little information about this functionality and that is one thing that we cover today this is quite useful so what we talked about just taking you back so we started our discussion with an array we understood how to declare an array and also like we saw how operations can be performed on an array we understood how to get the length of the array and so that we can act upon it we can you do other things with the name then we also went through some of the ways of creating an array creating an array and then how to use stacks and the shift registers or shifters basically so I think that is all for today we will actually continue from this point in the next lecture until then thanks thanks for listening thank you very much