 Hi everyone again welcome to this lecture today, we will be continuing the discussion on tickle tk that we started the last time so this is citing like the third lecture on the tickle tk let us see what we did last time so we started like I mean the first lecture with the basic understanding of the tickle how the tickle works and how tickle was invented and all the things then last week we started in earnest understanding the tickle programming language we started with the tickle commands and how they are interpreted as I said tickle commands essentially like I mean so there is no command syntax or actually like no grammar for tickle essentially commands are nothing but words separated by white space and then the way that the interpreter works is there is a built-in interpreter which divides the entire command into various words and then it sends those words to a parser the parser actually then finds out what is the real function and that can recursively call the interpreter for further elaboration but it just a very very simple thing basically again I want to emphasize this because in the very first lecture we saw that actually we want a very lightweight scripting language unlike C C++ for much more heavy languages and essentially like again once again I want to emphasize that tickle is used to for run applications and extending applications run multiple applications also like extending applications today it has been a very prevalent use of tickle across various tools that you that you will use later on in your career there is not a single tool today that does not use tickle also want to emphasize that tickle is platform independent scripting language so unlike any kind of shell or anything you take and then they are dependent on the particular platform that you use and there will be like small quirks associated with those platforms there is a shell associated with tickle that is the wish that we saw very first lecture I didn't I forgot to mention about that in the last one so just remember that the wishes was developed specifically for tickle and in the tickle was developed at UC Berkeley by Austerhout who also wrote that book and if you look at the other book that I mentioned which is the Brent Welch Brent is a student of Austerhout and essentially he is probably one of the first persons who actually wrote programs in tickle so it though those two books are very good read so I would encourage you to look at those two books as so coming back to tickle commands the tickle command is as I mentioned its words separated for widespread the first word is a function and all the others are arguments only functions apply meanings to the argument as I said tickle itself does not have any kind of grammar so the function really applies the meaning to the arguments it is always it is single pass tokenizing and substitution and then there are two main concepts that being used from the last lecture one is what is called the variable interpolation or variable substitution variable substitution is essentially using these dollars time so whenever it sees the dollar it substitutes that variable with the actual value and then you also have the second one which is the second important concept is command substitution so I am just saying in substitution so the command substitution is used using these two factors whenever we see that anything enclosed in a square bracket essentially like I mean that is the first one again is interpreted as command and that is executed first and then the result is actually fed into the name of the expression then there are two ways of preventing the word breaks one is with the double quotes so whatever is enclosed within double quotes it is treated as one word and then we also have this the curly braces essentially like that also prevents interpolation so this also prevents the word breaks so the difference between these two is that the quotes will still cause the interpolation of the variable or the command substitution so the command substitution and the words substitution of possible within the quotes whereas if you put it in the curly braces then it prevents all the interpolation of law then the last one is we thought escape characters essentially escape characters are for special characters so if you escape them as even as a dollar then that will not cause any interpolation printed as this again I want to emphasize that tickle is a scripting language usually takes strings as input and strings as law so it does not attach any kind of type to the data the way that type is provided is through those functions that we will talk about more and more so after this we also looked at the tickle expressions as to what are the rules for the expressions one rule that comes to my mind is this floating point versus fixed point so whenever we write the fixed point expressions the tickle also like considers the result as a fixed point whereas the moment we start a floating point doesn't matter after that how many fixed points we use the tickle expression will return a floating point number so this is something that you need to use it with caution so I will I will advise you and then I will leave it with that and then we also started looking at the tickle lists essentially so lists are essentially like I mean the collection of objects essentially and then we assign we can actually group those the elements in a list and separated by a white space essentially like the list is simply a string with list elements separated by white space we can use braces or codes to group the words with white space into a single list element which is very similar to what we saw here essentially so we can use the curly braces to group elements and treat them as a single element inside a list and then there are many many commands associated with lists a list command essentially like LISD will create a list and then there are several of the other commands that we will see in this section in today's section that will actually work with the list and manipulate and process the list so some of the commands we also saw in the last one are L index the L index essentially like I mean with an argument list and a number I will return the height element of from the list and L length list returns the number of elements inside that list we also have L range I think you can guess what it is and L append there are L insert which inserts exactly at where you want to insert L append will actually insert it to end and then L sort is another way to actually sort a list and this can take multiple options essentially what we can give like various switches called ASCII or integer real and then you can specify whether you want an increasing sort of decreasing sort things like that and then there are also like to concatenate a list essentially like we have concatenate multiple lists we have concat command we also have a join command for joining a new string into a list and then split string the other one which is essentially the split command is basically split on a particular string the list into two sets of its lists so I think like I mean these are the things that we talked about we will we will also like go through some some examples later on but I also wanted to actually talk about some of today's things on is the commands and lists essentially so lists pass cleanly as commands each element becomes one word but in order to create commands safely we use the list command itself that is the one that we saw here just list the reason for that is here there is one command the command itself is set x init value $ init value so this is essentially like I mean what is this unit value is read when the button is in now this command is fine we can also do this unit value in quotes so that it actually does command substitution but here the issue is if this init value is the two different words separated by blank space in between then it fails because now this variable that it expects is actually essentially our set x New York is not possible this command fails because this is the x that it is setting this value to and the init value is New York which it cannot set it so essentially like the command will pay here whereas if you say okay now set x and in quotes we gave the init value or sorry name curly braces we give the init value now what happens is if you have init value as one of the curly braces then it fails because again like I mean you know this is this is not a valid format because there is no escape character nothing for the curly braces so how do we specify the same thing where we want the command as set x to the init value which is space so that it covers any condition possible so what we do is essentially like now we say command here we specify the list and then we say the x as the init value instead of the ports are inside that the curly braces so here it will work because if the init value is curly brace then it basically it automatically escapes that curly brace and essentially gives you as that x as the escaped curly brace which will work so essentially using the list command is a very safe way to actually specify list files specify list so we will see this more and more again the one thing to notice tickle it is thrown to lot of errors and I mean you can make a lot of mistakes and make a lot of errors very easily and then quickly you can actually get bogged down into your own errors and trying to debug those errors so some of the safe practices when we say show it to you I think you should actually heed and very much try to adopt these kind of spacer practices which will enable you to succeed in coding in tickle so now we come to the strings essentially so we know that the string commands essentially are again there are many many commands for string manipulation strings again again the strings are group of characters that we talked about previous lectures and then string manipulation is one of the main thing basically and the string is the basic data item in tickle and as I also mentioned that the commands themselves are essentially like I mean the whole kind of parts as strings which is essentially a character separated by workspace and essentially like I mean using that we can do a lot of things and then the key thing to also understand is using strings we can do pattern matching which we will talk about and pattern matching is again one of the key elements in the tickle and it is slightly different than what we saw in Perl for that matter and the two main concepts are one is exact matching and globe matching this is GLOB globbing so glob matching and exact matching you will see like when what is this glob style and what that means so the string manipulation command so reg X so this is for the regular expression is also a format this is split string reg sub scan and the join these are all the string manipulation commands there is the basic command which is also a string and the way to use is string followed by operation and then string value and then any other arguments so string command similar to the list command that we talked about is the main command essentially like that is listed here this is the string command and then the string has the sub command which is this operation that can be compare first last index length match range to upper to lower trim trim left and trim right so we will see like some of the examples of how to use these commands one quick notice all the indexes within the string start with 0 so if I say fear academy so this is string value 0 0 and I is also you can say and okay so it starts at 0 and then end means the last character in this example and notice that actually it is one word so if I split it here this is a string this is another string and for this string this is 0 n or this string this is 0 and this is the end so it actually goes like 0 1 2 3 if you make it continuous then exactly 4 5 6 7 8 9 10 so the value index 10 is also same as index n okay so now let us look at these commands the string compare takes two more arguments which is string 1 and string 2 and that returns 0 if they are equal minus 1 if string 1 starts before string 2 or it returns 1 so just this compare as 3 return values and then the first also takes two string two arguments string 1 and the string 2 and then that return the index in string 2 of the first occurrence of string 1 if the string 1 does not exist as a part in string 2 then it returns negative 1 to indicate that the string 1 is off and then the same is last essentially which is the last occurrence of particular string 1 some of the other ones that are interesting are range range gives basically it also takes two additional actually like three additional arguments one of the string and then two numbers and then it returns the characters between those two numbers as well and then the string match actually that takes a pattern and also after that it is a string so takes two more arguments of pattern and the string and then it returns 1 if the string matches the pattern otherwise it returns 0 and here the string match command this one uses the globe style magic that means that you can use special characters and wild cards we will actually see that for the example then we all we can also like trim the string essentially which is the it trims from both ends of the string and then the character usually like I mean it also has an optional characters that you can take and that is the characters it will trim and optionally I mean for as a default the character is always white space which is it trims the white space from both both ends if there are any and then the trim left and trim right does the same thing on the either side the two upper and two lower is essentially to upper case the string or the lower case so now let us look at the key thing that I mentioned previous one which is the globbing so the globbing and regular expression essentially the globbing is a simple pattern language so I want to emphasize this whole thing which you want to use in globbing style which essentially like I mean which with which you can really use those two those four times first one is the star that means any sequence of characters so if you want to match say a star that matches here a pattern any kind of spelling or just CRA that is fine so anything that the star will specify that this is here then we have the question mark question mark matches one character at a time so how many ever question mark you give it matches that many characters for example if you say yes question mark are Academy then it won't match here Academy because you have two characters in between and it only matches one of them so that will match SIR Academy SIR Academy S you are Academy whichever way that you can spell it will match now we can also have square brackets and some characters in the middle this can be characters or a range of characters and here it matches those specific characters and it matches one character in that one or in that range again you can have like the multiple of these essentially to match more than one character but every square bracket will match one character out of that set the set that is this idea and then we can also like backslash with the C is matches see even if it is all these special characters so if you want to exactly match a star you basically escape it as so that matches exactly that character so I wanted to make a distinction between the globe style pattern matching and the globe command that we saw in the earlier months before that here are some of the examples so start out exe that is specifically this look style which is this one and then we can also have a through e star that is first character it matches ABCDE after that it can have any number of characters and with the extension text here we can escape the question mark which is basically so it needs to have a question mark then any number dot back is what is matched here so it will be a good exercise if you can actually identify these kind of things later on in the program now the globe command is also like another common command please do not confuse it with the globing style matching the globe command applies a globe pattern to file name essentially the globe actually returns the unique file names in that particular directory so here for each as globe star dot exe but $F is the program basically it goes to your current directory and look for any of the exes and then it prints out this message for those programs so the globe command is returns the unique files essentially whereas globing style matching is used to match the particular characters now if you specify like I mean so what is the alternative so this is this is by using string match now if you say string exact then this needs to have it won't match these stars all in all these square brackets etc so now a couple of more things about regular expressions one is this period that also matches any character the character matches the start of the string dollar matches the end of the string and then the backflash X is a single character escape and then the square brackets essentially matches any set of characters Tilda is not and essentially you can use dash to indicate the range so these are some of the things that you can use within the square brackets so if you do square bracket Tilda AE then it won't match the set 8 through E it is a range for the first character it will it can you can have anything like from F through Z or any numbers or anything like that in the parentheses and the regular expression matches the regular expression and here the star matches 0 or more proceeding the plus matches 1 or more of the preceding and then the question mark matches 0 or 1 of the preceding and then we can use this bar to divide alternatives so here are some examples of the regular expressions so here a through Z lower KT to Z 0 to move and then underscore and then when it is plus it is 1 or more this is a valid tickle identifiers so any kind of variable names are using this then here we say like tickle of TK with parenthesis and then using the bar T CL of K so that matches to tickle of TK so here another in the in this reg X command it is a reg X T CL TK I mentioned TK WT so this will return 1 which is a match and then the W becomes TK and then the T gets K essentially so if you recall the reg X command actually it gives basically the reg X reg X followed by flags which is optional and the pattern here and then the string and then the last two are the sub 1 and sub 2 which is essentially here what are the substrings from this match that gets added to it okay so now let us look at the reg sub command so that essentially like I mean so here it matches the TK here so the W becomes the TK which is what is matched here and then the T gets the substring K which is where the match happened now let us look at the reg sub command reg sub command again has switches here in this case it is the no case is the switch and then the pattern which is essentially like here it is Perl then followed by string and then we also have a sub spec essentially for this and a variable name so here the no-case Perl means match any case of Perl so it returns one a match because it matches this Perl and then the mantra is the variable name that gets this I love tickle as the string because once it matches this Perl it replaces the Perl with tickle and then assigns that whole string to this new variable mantra okay so now here is one example one quick question essentially like I mean more case varies it was the star escape question mark and then you have where is Bob and then we say basically like the subspec is whose and then escape one and then put it in result so how do we interpret this essentially like I mean so this is going to match varies some name any name followed by question mark so here the string is where is Bob so matches then so it returns one the perfect match now look at the parenthesis here so whatever is within the parenthesis will get replaced because this is termed as escape one so now basically the subspec calls for who is and then we places with this here and then that string is assigned to result so the result gets whose Bob so the Bob is the one that got matched and then this is the parenthesis that's called as escape one then that goes here so I mean give you like some more exercises to work on these concepts I think these are quite important ones or general understanding of Bob's 10 matching and less particular expressions now the format command essentially this is something that also let me talk about in the this one this does string formatting so in how do we format a string essentially review the the particular value and then essentially like I mean use we use it in substitution to that that particular thing so again here we say basically it's format and then this is what is known as the spec and then these are all the various values so this is value one and then you can have multiple values so every occurrence of this various characters get replaced with those values so here we say like percentage D that means that it is a sign integer and so 97 instead you can have unsigned integers we have O for unsigned octals X for unsigned hexadecimal C for mapping an integer to the ASCII character it represents S is for a string F is for floating point and then E is using scientific notation and then the G is in either the percentage F which is for floating point or the you know scientific notation whichever is shorter use that so essentially like I mean pretty much it boils with the format is as most of the print F cable is you can also use it to create the complex command strings now the next one scan is like scan F in C programming so the scan F actually parses a string according to a format and assigns the values to the variables so here let us assume that the string X as this information which is SSN and then we say basically like some number so when we say scan we scan this particular variable $X and then we say a format which is SSN hash is percentage D and then that is assigned to SSN now if you say like the social security number is $SSN this SSN basically gets replaced with this number here from here and then we can actually display that so again for scan command this syntax is scan string the same as the $X here and then the format and then followed by variable so here the format is percentage D on this whole thing SSN percentage D and then the variable is SSN so that is how SSN works so now we come to another interesting topic which is the control structures control structures are needed for controlling the various operations this basically gives the program the real structure or essentially the essence of the program is all the structure we covered this for Perl we talked about its data structures and then we went into more control structures even this is very similar the control structures are like C in tickle so if you know C it is very easy to write these control structures in tickle you will understand much better they are just commands that take tickle scripts as arguments again you should think again when we come to the tickle there is no grammar so everything is just a command and then the command will take other tickle scripts as arguments for it for example there is a list reversal where we set the list B to reverse of list A so how do we do this so here we declare a variable called B and then we say we set the I to the L length of A minus 1 can anyone tell me like I mean what what does this do so L length actually gives the length of the list essentially and then we need to subtract 1 from it because the first element is 0 so if you have n numbers the actual list indexes are 0 through n minus 1 so there are n elements it is actually each one is indexed 0 through n minus 1 so we subtract that one from that thing and then now we just go through this while loop which is one of the control structures and then we say $ I greater than or greater than or equal to 0 as long as the I is greater than 0 do this which is L append B to L index a $ I so again L index a $ I returns the character or the list element which is of I from the list $ a so now B becomes or B 0 in terms a n and then the increment I plus 1 sorry so increment I with minus 1 which is actually decrement a I so here now the next element will be B 1 because we go L append becomes a n minus 1 and so on so now you can see that actually like I mean it becomes a reverse and finally this is the end will be a 0 and that is when the program term so it is a good program to do the reversal of this but you can use this so you can see that so for doing the for controlling the commands on controlling the structures of the control structure commands are essentially if for which break for each while eval continue and source the source command is very commonly used essentially this just source a set of procedures or a new command so let us look at if else so here we say the variable X we set it to 2 and say that if it is less than 3 then put X is less than 3 if X is otherwise it says X is 3 or more so when you execute this program for X value of 2 now it will say this and it will exit it won't execute this at all because it is already exist less than 3 and then this is the while example we saw this list reversal essentially here the V is e being seen be after executing this program this is the same thing that we saw now we have far and for each so far usually we say set I 0 then $ I less than 10 implement I and then puts $ whatever essentially and then for each actually uses a list as its main arguments and it uses another variable in the middle and this variable essentially just taken through each of the values inside the list so for each color red green blue which I like color so it basically puts I like red I like green I like three states now there is another one which is the set a 1 as a a 2 as be a 3 a 26 as Z assume that everything in between is also set somehow now for each index array names a puts a and then the index there are two things basically like I mean now in an array so these are what is called array commands essentially array also like I mean we will go through this kind of commands but in array as an data structure the command is array and then you have names and values so the names actually return the indexes and then the values return what it is set to for example these so in this case essentially like I mean say index array names a and then puts a $ index now we can see that this program will output a the and so and then finally the command called switch essentially this is very similar to the case statement that you may be familiar with so here we see another example so we put these counts as 0 and then for each name we have several names here list of names and then we say switch and based on the reg X name so it is basically this name and then if it matches with Pete as the first character essentially then this is the first one and the number and then this is the main thing and then we increment these count and then if it meets Bob or Robert then we will increment the Bob count and then default is increment other count so now can you tell me like I mean what this program will generate as the counts so for the Pete count it counts one two so this will be two for the Bob Robert essentially it matches here one two and three and then finally like I mean the default is increment other count the other count is actually one two three four five six it goes to the catch all anyway so now you have six two and okay so let's look at some more rules on the control structures so here we have X is three and then we say if X is greater than two and this is okay because it is evaluated only once but if you say while X greater than two and then you are setting X to three this is just an infinite loop now here we have a set a as red blue green the list of red blue green now say like we say for each the eye dollar a this is an okay syntax but if you say like I mean same thing with the red blue green then this is not okay and this is essentially it basically just errors out because it just gets confused as we know that for each has just two arguments one and two so once it seems like more number of words it basically comes out and then for each array names a this is a common EDM in tickle so now let's look at one small example which is just a fun example for all the I mean so this is the control structure that we all familiar with and then very easy to understand to so we look at this one and then we look at some complicated examples in the next lecture so here first of all what is this what is this program this program identifies person as either a child a teen or adult how does it do so it asks for a question how old are you and then it gets that the value so it takes it from the standard in and assigns to this variable called age then it just examines that variable so if age is greater than or equal to 0 and if age is less than or equal to 12 then it prints out as your child if age is greater than or equal to 13 and age less than 19 then it turns out that age in your team and then finally if age is greater than 19 then it says that you are an adult so a simple program so how can you write with say switch statement the one switch statement and number 2 is what happens if a input age less than 0 and then how can you make it robust let's discuss this in the next lecture so I want you to go and work on this one essentially first of all modify this program into using switches this switch basically which is essentially which we want about it here and then the second thing is what happens in this program if you put age as less than 0 what is the program of it try to find out and then the last question is how can you make it robust meaning how can you power this condition if this errors out or doesn't have a how can you correct power this condition and that also you can use the switch command essentially so let's pick it up from this point the next lecture thank you all you have a wonderful day thank you bye bye