 Once again welcome to this the next class on the Linux programming and 15 today we will be continuing the programming language so before I go into today's topics let me just recap a little bit as to what we have done so far. In the first lecture of Perl we started by actually looking into some basic language constructs we wrote programs to write like hello world we also kind of understood how Perl reads the files or Perl reads input from the terminal and then outputs into the terminal. We also understood a little bit about the basic data types that is a scalar data type and then an array data type array also we introduced something called associated associative array and essentially at that time we said that basically okay string is another type of an array and then basically string is again an array of characters and that's why it's like another array. And then in the last lecture we learned a little bit more about strings as a data type basically we learned that we can have strings of any length the entire book can be considered as one variable a string variable and then we also did some string matching I hope you remember the s flash and then basically the first matching that we learned was equal to followed by the tilde of the wiggly character that's on the one of the keys in the shift keys in the keyboard then that wiggly character along with that we said basically if you add an s then we can do substitute essentially we can not only match but also we can substitute a character or another character then we also did basically some other types of matching one is the transliteration that we talked about which is represented as TR and then here essentially we can change the characters from one case to another case or substitute for some characters with some other characters things like that there are a lot of other types of adjustments that can be done then we also talked about how to open a file and then read a file and then how to close a file we also talked about how to write into a file using the ampersand and then we introduced the basic concept of what what is the file handle essentially which is essentially the pointer to the file using which Perl actually writes or understands the concept of a file. So we also know that there are specific files that are already built in functions which are the less than STD IN greater than or less than STD out greater than and then you also saw the less than standard error STD ERR and greater than those are all for the predefined file handles which is which are essentially used to read from and write into the terminal or essentially for the program so other than that if you are defining a particular file handle we need to first open that file and assign a name for the file handle and then once we assign the name then they can use that term so so that that's the so that that's the thing that we learnt about that and then we also actually saw some control structure so that the control structure that we learnt was basically if then else if else is else basically that is the if then else construct and we learnt a little bit about how to write those things and then we also started talking about the function calls essentially for the function calls essentially like I mean this is something that basically we know that function call can be specified basically like it as a sub and then the function name and then we can also specify some argument and then once you write the function and then we can call that function into the program by using the ampersand function then we learned about some escape characters so these are mainly for escaping special functions and here actually like the kind of distinguish between the string with like just the code as well as the double codes so the key difference is that inside this one anything that you write is just literally translated or literally kept whereas here you can have additional variables and then variables will be replaced at the runtime program and then we also talked about a small quiz which is what I wanted to talk about in the first thing I actually asked you what is the meaning of this the backslash backslash n as you know like the backslashes are basically the escape characters essentially so within the code if you specify backslash backslash n what happens so I also asked you to actually try to run this program basically the dollar a equal to single code backslash backslash n then dollar B is equal to the double codes dollar a and backslash n and then now you print dollar B what happens so I hope you tried this out if you have if you haven't you can still try it out but I am going to give you the answer essentially today so the answer is actually for printing the dollar B it is the backslash n basically this is also equal to backslash so as you know like the backslash n we studied in the escape side basically just specifying backslash n means that it is a new line character but in this case since we are escaping the backslash it is basically this is treated as a literal and then this is treated as a literal even the any possible as a little as you know like I mean within the single code only the backslash and the tick or the apostrophe are the only two characters that can be escaped and it happens to be one of them so this is just a little backslash n so even if you just print this out it will be backslash n even if you are using it within this variable and then you have to dig it out it is only backslash n the second backslash n makes essentially treated as the new line so the program goes to them or the first that goes to the next line I guess this is clear you have any other doubts please feel free to email me or PA who should be able to answer these 10 questions so with that we will start talking about today's topic essentially today we will be continuing our discussion on the various the the the we are continuing from the escape characters we will go into more operators today and then we will also look at some of the scalar variables start talking about the scalar variables we need to do the scalar variables in the couple of lectures ago and today we will go more deeper into that scalar variable as I mentioned the one of the goals for this course is we will start slowly building things so that by I mean we are not going to be like I mean we will cover some of the basic information and things like that but we will start building this programming knowledge right from the get go I want you to study all the basic stuff and basically like every topic one by one and then at the end you can put together and write a problem that is not the internet of course as you go through the course you will be able to program smaller bits of distance pieces of power which is essentially you know the goal for somebody who takes the book is intuitive so that you can start coding even you don't have to go through a course of many many days before you start coding basically as you learn we build small small scripts and then we will build it up and then finally like and by end of the course you will be able to do whatever programs that you want very easily with both so let us look at some of the operators so the upper operators in Perl is a superset of the other languages that are there the C I will go and Pascal basically it also provides automatic conversion between the strings and numbers there are arithmetic operators so operators are not limited to just this arithmetic operators but the arithmetic operators are essentially addition subtraction multiplication division modulus and exponent C engine so we will study these operations and how to use these operators in this section there are also like relational operators for numbers and spring they are both a separate one if you have a number data then you use one type of relational operator and for strings use a different type of relation there is also a concept of this concatenation operator concatenation means joining strings so if you have like many strings that you want to put together there is a dog in my house and each one is a separate string you can put together using the dot operator and then for strings they we also have a repeat repetition operator just the X which is very similar to the multiplication operator here the repetition operator essentially like works on the strings and basically it produces multiple copies of the string we will see like I mean how this and you know the regular multiplication operator is this as so star symbol which is shift 8 in your keyboard so we will see like some of the examples as to how to use them and before that let me talk about the relational operators this is the table that we saw previously in making like a couple of classes ago I want to read it once again so in the relational operations basically we have set of one for numbers and one for string so this is the number one number relational and then this is for the string relational so equal to is straightforward this is to compare for eq it is basically like two strings whether they are equal this one is like if two numbers that are equal so this can be even when I say number basically then be like variable so you can say like follow x equal to equal to one this compares whether it is x has a value of one and here like you can also say like Y eq my name something like this so that this compares with the dollar is my name and then there is something so this is the eq operator the less than symbol is for the less than greater than is for the greater than and less than or equal to these are like some of the things that are provided you may wonder like even how do you use like less than greater than le you know g and all for the string operations again the equivalent numerical value is used like so for example like you can actually have a string which is like 12 so if it is a string operator then you would say like I mean my dollar number is less than literal 12 so here essentially like I mean you can again compare a number against the literal string as well so same thing like I mean the greater than or equal to and then finally like not equal to we will use some of these things in various programs in the coming lectures you also have some assignments using these concepts so here are some examples so 1 plus 2 straightforward it is 3 this one 9.8 minus 7.6 it is 2.2 again you see that basically you do not have to explicitly specify floating point as you know for everything is this floating point 3 times 21 63 again easily understood 14 divided by 2 7 10 divided by 3 is a floating point number 3.33333 in well has basically the position is limited by what your computer allows and then the 10.2 divided by 0.3 34 these are pretty straightforward operators essentially like I mean for addition operation subtraction multiplication and division so these are the basic operators now let us look at some other operators as well so this one is the modulo operator so 10 modulo 3 is 1 which is the remainder essentially and then 10.8 modulo 3.2 is 10 modulo 3 same thing and this is the exponentiation so 2 power 3 is actually 8 so here if you try to do this 17 less than 7 the answer is false whereas 17 less than 7 in the string operation the answer is true so some of these things basically what operator use is different so you are a numerical one then you use this whereas this one is used for string operation this is mainly because they can be compared the first one and then it produces this answer and this is the concatenation operator the dot so it produces just a single one no space here and things this is joined together and here essentially like I mean the you can look at this one as you shouldn't have this w that is the typo but you can see that actually it concatenates a literal string with the new line and basically it combines into one so now we look at some additional things basically like one thing that we talked about was repetition operator which is the x operator so the x operator essentially multiplies that hello by 3 so you get this as the output then the world x 2 plus 1 and 2 plus 1 is in parenthesis that prints out world world world so the parenthesis is actually operated first so 2 plus 1 becomes 3 and then 3 times world is essentially the same here again when we use the x operator the 3 plus 2 is evaluated and then that is essentially 5 and then 5 times 3 so it is 555 and 5 is treated here as a string or literal but at the same time 3 x 3 plus 2 is actually 3 which is some basically this 3 3 plus 2 is evaluated first which is 5 so it just repeats 3 5 times and hello times 2.5 basically like only like 2 times it's printed out so the last one is the somatic and if you do it anything less than one basically it's nothing is printed out so I think this is clear you can actually play with this these operators to see like I mean what in various outputs are I'll also give some exercises on the form to work on these operators to see how you can make use of it. So now let's go into residents one thing that I talked about here was this parenthesis basically like how parenthesis changes the presidents basically instead of world times 2 and then plus 1 it's actually like the 2 plus 1 is evaluated first and then world same thing like here we see like some presidents operators okay so how does well understand this president are there any rules behind this how to evaluate operations so that's what we learn in the next section basically the rules are fairly simple essentially for all the operators found in C the operators have the same president as in C so this includes like plus minus the multiplication division etc and then you can check a table for the lower to higher presence I will talk about the table in the next slide and then for the operators at the same level it's resolved by associativity okay so there are two ways basically one is the table which goes from top to bottom and then we also have some rules regarding the associativity which is used to resolve for the operators at the same level so what do I mean by we will talk about this and if this is not clear like I mean actually there will be more chances that we will be covering this so here I talk about the associativity and then operators so the president goes from top to bottom that is number one okay so if you if you see like one of them here not and then one of them here say multiply so this takes the first president and then before this so basically like you have to evaluate the multiplication before you can use them out but what if there are couple of knots in the same line line so we go back here and then we say that basically the presidency is from right to left so you first evaluate the right most one for this one it goes this way whereas for and it's actually going and then R and XR again goes left to right and then so you can look at various things one is various operators here with some of these things we haven't actually like one this one we have learned which is the exponentiation which is again going from right to left so this way okay and then this unary operators all the same thing basically whereas some of the comparison act operated goes from left to right here it's again like left to right all these things like there's a comma operator plus minus they are all going with this and then you can see that actually this plus and minus have a lower president than this multiply and multiply and divide so if you have the plus minus then essentially no that will that will happen later than the multiplication and division which is the normal rule anyway one other thing to note is basically the parenthesis will change this president order so if you put anything inside parenthesis those are evaluated first and then before signing this present state so there are all the operators that are mentioned here have this president so I want you to go through this I have some examples on the next slide which we will go into now so here are some examples so the change there is then is a function call chdir and then basically the dollar who is some variable and then we specify like I mean okay if this condition is not true then die that's the meaning of this particular statement we will talk about these kind of statements in the next section or in the coming section but I want you to pay attention to this one so here we specify this without any questions basically straight that the same as writing first the chdir who and then the or die so this and this are exactly the same if you put a parenthesis inside the the school and then doing the same thing that's again the same this is also exactly the same there where we basically like change there with no space and basically just straight away declare the dollar foo as its argument and it's the same thing here and then finally like even if you say like change there plus the dollar flu foo which is an addition operator and then say like die and then again that's exactly the same that's because the addition takes precedence over this relational operation so this relational operation I should say one thing on so just in case so now when we use a multiplication operation instead of an all for example here then this is same as the change there with dollar foo times 20 because now this one has higher precedence than this relational operator so we go back to the thing the relational operator here that's up a lower one than this the asterisk so again you see that basically how the presidents work so again when you do the chdr dollar foo that's the same as before basically so here you can see that the first statement and the second statement by the same as your first statement second statement is different but the second and the third are the same because now the parenthesis takes precedence over the star and then essentially like I mean it basically it so that dollar foo is associated with change there instead of train in the first statement without any parent the system modify the presidents basically the foo is assigned with 20 another interesting one is this one here we put the parenthesis around foo but we add an additional operator so you can see that actually like again this has a higher precedence than variation so it is again it is foo times 20 and that is changed then we are actually generating some some random numbers essentially so this is random 10 times 20 which is essentially random 10 times 200 I mean 10 times 20 which is 200 so it generates a random number between 0 and 200 here and essentially you can see that actually that's how it is taken whereas if you say like grand 10 in parenthesis times 20 then it generates a random number between 0 and 10 and then that it multiplies by 20 so you can look at this one basically like here there are maybe like 200 combinations and out of that one is chosen here you have only 10 combinations and all of that one is chosen so the answer maybe like still like I mean the range is same basically this has 0 to 200 this also has 0 to 200 but you have only like 10 different values whereas here you have 200 different values and it is choosing from one of them so these kind of precedence metrics and before this discussion will help you to write programs which you want to do it properly because in this in this section basically you want to generate a random number or some task which you think that basically which is a combination of two different types basically and then you choose to represent this way which is the correct way but if you are doing this way then you are actually like I mean you are not actually getting the full range to generate the random numbers because here the random number generated on 20 40 60 80 or multiples of training whereas here it is multiple of one so it is one two three four all so that is one way of looking at so depends depending on the applications you can really generate and sometimes like I mean if you do this then the reduce set even though the range is same you are actually biasing it with only like 20 different values so here is one quick question again regarding the same presidents that we talked about here essentially like I mean I am declaring an array and array contains values one three then I introduce a function called sort sort is you can think of the sort as sorting multiple values then I give like four and two and then I say print dollar so what do you think will be the output of this problem so I am going to give you the answer of course for this one because it may be good to actually practice these kind of problems because again this is the kind of things that you can do because world is a very flexible language so there aren't like hard and fast rules on things basically you understand how the business work so that you can write the appropriate program this also actually adds some additional complexity to the language and which makes debugging pretty hard because a person may have written one way his or her algorithm and then if somebody else wants to debug it now they need to understand what was thinking behind writing the program before they can modify it so again it becomes very hard because all these complex syntax how do you actually sort through those syntax again I want to advocate for a program that is very clear and concise but at the same time you know the when you go out and appear in interviews they will ask you to the understand the intricate intricacies of Perl so I think you have to know this but in practice make sure that you don't really complicate the programs and keep it simple so let's see what the output is so the answer is 1324 so here there are a couple of things that I want to note out here number one implicitly the thought is actually applied here but within that basically like I mean the the comma operator gets the president so first the comma is operated which is like now we get only four and two and then on which the source is operated so you get the result as 24 then once that is performed then you have the other two comma operators so the comma operators are just basically more like one so the array is actually comprised of just one three two and four answer is this I think you can actually run this program and check it out whether you get the same answer or not okay so let's move on so as I mentioned basically the operators can be operated on both strings and also the new numbers so the string is used in a numerical computation automatically convert converted into its equal and moving value which is a decimal and the floating point value decimal floating point value so couple of things to note one is the leading white space characters are ignore so if you start with blank number and the blank is you know only the number is taken and then the filing non-number characters are ignored so if it is in umber space in umber that is basically converted as just zero so that is a non-number string is the number zero so the numerical value is used in the string operation it is expanded into whatever it would be printed okay so um so let us look at some examples so here in quotation it is one two three thread times two what will be the output for this so it is this is the same as just saying one two three times two because the leading this blank this is ignore and then the trialing all these non-number characters are considered they are taken to they have just changed basically they are ignored also so the result is just one two three times two and the reason why this is the true is because of this if you say like I mean one two three thread x two then the answer will be like one two three thread space and then again space okay let us look at one more example so what will be the answer for this so as you know like I mean the pan this is operation is the first and so time is performed that has the highest president along with it there is a star so this has the next president so three times 14 three times four is a equal to first and then the answer is 12 and then that is concatenated as a string to x so you get x 12 and this is the continuation operation so the bottom line is like I mean most of the time we do not have to worry whether we have a number of a string because we just perform those operations and basically let me just see what is the outcome the only issue here will be when we use like relational operation that we saw earlier 17 is compared with seven actually the 17 is greater than seven so that if you are saying like 17 less than seven there will be falls but in the string terms it is actually like one is compared against 17 you will say LT seven now one is compared against seven first and then seven is compared against whatever is next year but here there is nothing here so seven comparison is not there only the one is compared against seven and one is really less than seven so this becomes true so when you are writing scripts you got to watch out for what exactly you want the results to be because if you do something like this and use the wrong or relational operation then you will get a result but which you would not expect and then as a result that will cause issues and it is harder to debug okay so let us also introduce for the scalar variable we actually went through this a little bit in the first lecture the scalar variable holds a single value it is either a number or a string the general syntax is essentially letter or a digit essentially with a prefix of dollar and then you can actually have an underscore also so other characters are not allowed in the variable and essentially like I mean so it basically rejects the additional characters and it is key sensitive so you can say like I mean same thing like I mean the other name is not not equal to dollar me okay this is true in both this as a last you can have different values for this and then for the name itself there is no length limit so you can have a however you want so let us look at how we can use them basically one way to use the scalars is through an assignment so you can assign values to the scalar variables and which you in turn you will be using it for various things essentially so for example here a typical assignment will be dollar a equal to seven and then we will say like okay dollar b is equal to dollar a plus seven so here you notice actually like there are there is this is the quality operator so here we just assign a value or assignment operator where we actually assign a value to this particular variable this is also some sometimes we can call it as initialization where we first assign a value and then we can do some operations on it here we assign dollar a plus seven to this one so if the dollar a changes same to nine at some point then this that will be reflected here so currently this is equal to just 14 but it can change based on value and then here you can see basically that dollar c is weak concatenated with dollar b so can somebody say what will be the answer for this one it is that is weak 14 so that is the way that we can write it and then here we say like d equal to in parenthesis is equal to 3 plus 4 so now what will be the value for the d so here essentially like I mean you can think of the time this is actually change the president so you evaluate this course and then so in when we evaluate this this is an assignment operator when 3 is assigned to a and then that plus 4 basically becomes dollar d is equal to 7 here it is basically like a continuous assignment essentially around here is assigning a $f which is $7 so both of them will be 7 so I think this is the way that we can change the presidents essentially and also we can move some operations basically on the the various variables the scalar variable is one of the most widely used variable in Perl pretty much and most of the operations that is using scalars so the scalar variable we will learn a lot of things about the scalar variables in the coming lectures so let me also go into the binary operators essentially or binary assignment so essentially this is very similar to see if you more see them this will be fairly easy the order of evaluation is unspecified so if we go back to our the metrics that we talked about here so the the the some of the plus pluses are there basically very similar to this one but they do not have any kind of associated so so now let us see like I mean what that means so here a equal to 5 and b equal to 5 so it is a standard that is given so now we are assigning a plus equal to 7 first of all what does it mean this means actually the a equal to a plus the reason why this order is an evaluation is unspecified because it does not matter how this this this thing is evaluated okay so in this case the dollar a will have a value of 12 because it is basically that is the value that it settles that okay so whether this is evaluated first this is evaluated because it does not matter this is so this introduces some kind of ambiguity later on you will talk about that actually so and then now we come to this basically so here again same thing so this is like $B is equal to $B times that that is why 35 and then there is also like this concatenation operation which can always be a binary assignment which is a lot of people use this shortcut for binary assignment where they $ string equal to day days and then they will follow string not equal to of life so then now the dollar string will be equal to days of life something like that so so it is a it is basically like I mean we can actually create new strings very easily by using this binary assignment so now we come to this last example $B is equal to $A plus equal to 2 and then $A minus equal to 2 so what will be the answer here basically first of all this assignment is the worst assignment so never never do this app it is bad why is it bad the reason why it is bad is because inside this whole thing this order of evaluation is unspecified so we do not know like I mean whether this is evaluated first so now how will you resolve this and what will be the answer I want you to actually test this out before I give you the answer because again like I mean this is one of the key points of understanding that we should be able to understand how this is this is done so I am going to actually stop at this point I will do a recap of what we learned today and then we will pick it up from this meanwhile I want you to experiment with this particular assignment see what happens and explain why you are getting that answer so first of all find out what your answer will be you do this so follow this this one basically $A is 5 $B is 5 or actually like just make it $A as 5 and then just do this operation see what you get because again this order of evaluation is unspecified what does that mean find out and explain to me in the next class what happens and in fact I will also explain to you as to what you should have seen and why you cannot see that okay so let us just recap what we learned today so we started with some operations basically we went through like the various operators essentially in Perl we have a number of operators basically it is a superset of C although one asks how and there is automatic conversion between the numbers this we saw basically how we can do this and in Perl actually there are arithmetic operators addition subtraction multiplication division modulus we saw that that is the percentage and then explanation which is the double last and then we have the relational operations and one set for numbers and one set for strings actually you do not have a data type where we can operate these things on you can easily operate the spring relational operator on the numbers and then the number operator on the strings even though like I mean when we operate the numerical relation operators on string on strings it can complain saying that hey you are using rocks in that field but if you use the string comparison for the numerical society for alternative issues then we also looked at the concatenation operator for strings then we also studied about the string operate string repetition operator which is the x basically for multiplying numbers then we went through all these various operators we also studied some of the president's order so this is one of the key things that we will again revisit in the next lectures in presidents essentially like when we said that basically the Perl the order of president is first of all it goes from top to bottom and then it also follows whatever the associated rules that are specified in for the same operators you use this just this one row essentially so and then and then that also varies essentially for some operators it's right to left and some for some operators it's left to right for example the not functionality is actually from right to left whereas and and an R and X for all of all from right I mean left to right so this we learned and then we learned also like in how to answer some complex questions then we studied about the conversions essentially which is how can we use string in the numerical computation now and then how can we use the number for comparing in a string way essentially one of the conversion thing the rules are essentially here one is converting from a string to a number the leading white spaces you know and then the trialing non-number characters are in and then whatever is remaining that's your number essentially and then the numerical value used in the string operation basically expanded into whatever it would be or when it will be a print when it will be printed so this is something that we also saw and then we went into the scalar variable essentially like these are scalar variables are variable hold a single value it's usually represented dollar followed by number of letters or digits essentially so you can have a dollar one to three as a scalar variable that can hold some value even though we don't want to we always typically have a letter essentially as a starting point actually here also like in syntax we start with a letter then we can have any digits or letters or even an underscore so we'll so the dollar one to three is not allowed as a whereas dollar a one to three is okay so again the the scalar variables the variable names are sensitive and there is no length limit following the variables so I want you to keep in mind about these rules and this syntax is syntax basically writing the thing and I will ask you to follow like one type of syntax again so pearl allows this underscore and people typically write like name or me underscore one to three or you can specify like dollar mine my pearl so they use underscore to link various words to form a variable you can also use camel case because it's a case sensitive and basically you can say like my pearl as one word of me one two three so I you can choose one style of the other and usually like you can choose a style and it speaks to them for life so to speak and people who are well versed with pearl they typically choose this underscore so I would like you to choose whichever one that you are most comfortable with in representing various variables then we also learned about assignments the two types of assignments one is a regular assignment which is basically assigning values to a scalar variable we use like various operators to achieve that assignment so assignment you can think of that as an operator which is represented by equal to and then we use various types of assignment to feed into that particular variable on the left-hand side so we also learned about this binary assignment operator which is these kind of operations essentially which is two operators linked together mean here also so basically one thing is the order of evaluation on the offense is unspecified for which one is valid first is not known even though like I mean most of these kind of cases we evaluate basically you know it's same as seeing the equals eight to seven so the plus is evaluated first before the final so a will get the value of 12 or b will get the value of 35 but when you use it with these kind of complicated they there this you are using one type here and another type with the same variable the value sometimes is not what we expect so I also gave you this example this will be a quick quiz for next time to see whether you understood this run this command and see whether what the result you get and then you can share it with me I will also share my result with you next time and give you an explanation as to why I see the value so hopefully I think this is not there we are getting like more and more deeper into so thank you again for listening thanks a lot see you next time thank you