Tip:
Highlight text to annotate it
X
Hello perl class, this week, we are going to talk about control structures, foreach
loop, other loops, logical operators. We are going to go through the class notes together,
and here it is - Control structures. First, an example of the great foreach loop - I call
it 'great' because, it most likely is the most favorite construct loop of perl programmers
- very intuitive and you see it everywhere. So, first, we are going to create an array
and we're going to populate with numbers from one to ten. Here's our function foreach - in
the parenthesis, you simply supply a list, in this case an array and you just give the
name of the array. In between, you are intercepting the data, coming one at a time, each item
coming through the array here, you are intercepting each item with a variable of your choice - in
this case $item. Anyway, simply going to print the $item in the first argument and followed
by a string and here we go - let's give that a try. Now, remember that to run this, it's
usually best to keep this window open, save the data and not exit, so you keep your cursor
in the right place, and use another window here to run it. Here it is, 1, 2, 3, 4, 5,
etc. with a return character after each. #2 - item, which is of course, anything you want
to call it, is an actual item in the array, meaning this variable points to an actual
element in this array. And if you modify this variable, no matter what it's called, you
will update the array, the element in the array itself, so it is a 'destructive process.
For instance, we're going to use $item here to intercept every element from the array
and now if you multiply $item by 2, it will take $item and multiply it by 2, it will actually
change that item in the array. So let's go see if that is the case, we should have multiply
everything by 2 and here it is, the array now contains 2 4 6, etc.. #3: the foreach
works with any list, so, obviously in the parenthesis, since the list is nothing but
an array, we can also produce a list directly. We're going to skip that one. #4: there is
a special variable we may have run across earlier, it's called the $_. It's a special
variable, and it's a default control variable or argument for many functions. So if we don't
provide, if data is being passed on, and we don't supply a variable to capture that, perl
will capture that data automatically in a special variable $_ and oh, before we do that,
here's a little aside here, this is interesting, if you print an array, inside of a quote,
as we are doing here, the print statement, will use spaces, as you've seen, but interestingly,
if you print the array by itself, not inside of double quotes, then you will get no spaces
(it's just interesting to note, let's try that out - and here it is, in the quotes with
spaces in between and this prints just like a hash, doesn't it, no spaces. #5: So here
we go - if we don't specify a scalar variable, the special variable is assumed, so, here
we are, passing this list of strings, separated by commas and you notice that we don't specify
a variable here, in which case perl would put this data into the special variable. Here
we are printing a space and here we are even printing nothing, so it looks like, so, we
don't provide a variable, so perl will assume $_ and let's see here .. it looks like it's
printing it once, and we're gong to print a return, and then we're going to print, what?
It looks like we're printing again whatever may be left over and then we do a foreach
loop here and we're going to print specifically, this time, the $_. So let's see what happens
- it should print this list twice, right? Yes, so here it is, printed once, a return
character and now printing this list, this time without any spaces or returns. #6: function
grep. You need to be familiar with the grep function in linux or unix, right? In perl
we have something similar and this is to help us understand what it does, we can simulate
the grep function with a foreach loop - we provide it a list, and what the grep function
does, it puts the list through a condition and only filters the data, which satisfies
the condition. So, here we have the condition if it's a push, we're taking whatever element
is coming out of the list and we're pushing that into a new array, if it meets the condition,
and the grep function itself, right here, has two arguments, the first one is that condition
and the second argument is the list itself. So the list is passing the list to the condition
and grep will be forwarding that information if it meets the condition. An example here,
we're going to take the grep function. Our condition is, of course the $_ is going to
be less than 6 and the numbers are going to be coming through, one at a time, 1 to 10,
and as long as there are still under 6, then grep is going to push that information to
it's left into a new array called low_numbers and then we're going to print it - and here
are those numbers - 1 2 3 4 5, and nothing greater than that will come through. #7: the
brother/sister function of grep is map. I don't use these a lot, but if you're going
to work seriously in perl, you'll notice a lot of code using map and grep. Serious perl
programmers love those two functions and use them in all kind of ways. If you put both
of these together, you can practically do anything you want. It's very concise, very
powerful. The map will similarly, except this time instead of having a condition, the map
provides a function, so instead of filtering the data, it's going to manipulate the data
somehow. The list is still on the right, it's going to go through that function, the numbers
will be modified and the map will pass that to a new list, here is the example: we're
going to take the numbers 1 to 10 and we're going to process them through this function,
which is simply in this case, very simple, multiply every item by 2 and the map will
create this new list and we're gong to print the list - and here it is, all the numbers
have been doubled. And you can also, print that directly, you don't have to put that
necessarily in an array, if you just simply mean to get the information out. OK, and what
is this, an exponent, right? ** .. and we're going to map still produces these values,
and the print statement will take those one a time. OK, let's leave that one. Off topic
but mostly useful for using system commands so let's talk about that for a minute - you
can print the content of your directory from perl simply by using these brackets and of
course the asterisk, which prints everything. That list is going to go into .. note, this
is another , the equivalent of the $_ for the list. So, we have $_ and we have the @_.
we'll run into that one later. For now, let's see what we have in my directory here - Sorry,
it looks like I may have forgotten to take an exit here. Ok, and the directory is ch3.pl,
etc., I have all these, it's just a crude printout, of course, since the array is doing
the printout. Quick format of an array printout, this is, oh yes, this is interesting - here
we can use joint - this is another function we might as well get familiarized with - two
arguments, the first one is - what would you like to .. it's going to take an array, which
is the second argument, of course, and join, meaning it's going to create a string out
of the array and every element in this string will be separated by some character, which
we supply in the first argument, so in this case, we're saying, take every element, put
them in this string but separate each element by a return character. So here is the join,
the join can of course will supply this string that it has put together and we're going to
say print an array join: - not sure why we have all these extra semi-colons, it's not
very pretty. So, let's print this out: and remember, 'join' will turn an array into a
string - very useful, an now the directory looks better. Printing two columns with map
and the conditional operator - Let me skip this, it's an interesting little - we'll see
if we have time for this, this video is getting awfully long, but I would, this is just for
fun, and it also talks about this important statement - this is an if statement on one
line and it comes in very handy sometimes, the way it works, it's an old if statement,
left over from older languages - we have a condition, and then we have a dollar sign,
following the dollar sign, is whatever you want to happen if the condition is true and
then you have a colon and after this character will be executed if that condition is false.
So this is a one-line if statement and believe it or not, it's still used a lot and it will
come in handy. Let me skip this for now, but you ought to take a look at it, see if you
can figure it out. We can talk about it on the discussion board if you have questions
on that. Change directory - we have these commands, of course, use these, whenever you
can system commands of course, but those are more stable, especially if you are going to
port the code to the DOS world, so you can say chdir and you know what that means, of
course, it would go back one level and then we can go back look at the directory and we're
going to use a join function and print it out. You can change directory, this time we're
going to go back in the directory from which we just came and we're going to print that
again. You can use system commands, of course, but system function will turn your perl into
any shell command that you need, in this case, cp chapt4.pl to test2, is what we're going
to do let's see test2_chap, let me copy this and I'll probably have to remove it to make
sure it doesn't exist. Let's comment this one out, save, over here, let's do, let's
list here, and here is the test, yes, so let's remove that one, so it's no longer here, correct?
test_chapt4, well that one's here, let me see, remove, oh, this is test2 is no longer
here, so we're in good shape, run, and we can see back to the perl and we can see we
went back one directory and we printed all the things I have in my root account and we
now have test2, I don't see it yet, let's do a list, yes, test2 has been created. OK,
that's it on that topic, there is one important topic I would like to make part of this video.
Counting frequencies - of survey results, the way to tally values with a hash is demonstrated
here, very important. Be sure to understand this fully, we will be using next week. And
it's not only for the fact of doing an exercise but counting frequencies is something we have
to do all the time and hashes work very well for this purpose, so you need to understand
this very cryptic little code - it's very simple foreach loop, which will take in one
line of code, it will know exactly how many 1's and how many 2's we have in this list.
So, take a look at this, make sure that it makes sense. We're doing a foreach list here
and we're going to be using here the simple special variable. Now this hash is created
on the fly and notice that we are creating a key, right? A key, of everyone of these
as they are coming down the pike. And now many keys to you think we are going to end
up with? Well obviously only two, there are only two possible numbers here. And so, the
first time this runs, the number 1 key will be created, and notice what's happened to
the content of that key is being incremented. The second item is going to create a new key,
called '2' and it's going increment the content of that. If you don't initialize a hash, and
you increment the content, perl will automatically assume a zero and increment it to a one. Next
time this key comes up, it already exists, right? But it's going to take whatever content
and usually replace it but, now we're going to increment it again, so the former 1 is
now going to turn into a 2 and every time another key of 1 is going to hit the location
of that memory cell, the content will be incremented and now we have two keys, one and two, and
by the time this foreach loop is through, you will end up with exactly how many times
that key has been hit. And we are going to sort by keys, which is going to be handy,
right? And let's see how that looks like: and here it is: the keys are sorted, 1 and
2. Now, let's go spiff that up a little bit, we can show the frequency along with each
key so we're going to say - foreach and we're going to sort the keys as usual, from the
hash and now we're going to print , remember the x operator, we're going to print as many
asterisks as it takes for how many ones and twos we had. So, first we are going to have
the key 1, right(?) coming through the special variable and we're going to look at the content
of that key 1 and that will multiply the x operator the asterisk and when that is done,
we're going to print a return, then the second key is going to be coming down the pike and
the content of that second key will act on the x operator and give us as many asterisks
as we need. So, let's give that one a shot - and here are the asterisks, the ones win
out! Next, let's make that better yet, we can add the key to the frequency. So, we're
going to say .. foreach , we are going to print this time the key, because the key will
become coming through first - we'll print the key, then we print the tab key, then we
go ahead and print the number of asterisk. Next, we can even include a label and this
is something else that is done very frequently, another great use for the hash - we're going
to call this key, we're going to make a new hash called label, alright, we can call it
anything, and that label is going to have the word 'yes' for 1 and for 2, we're going
to associate the key 2 to a 'no'. So now, instead of having just ones and twos, we can
easily give our keys a label. So, foreach, we're going to print first the content of
this key, in the label hash, so now the key '1', coming down here is going to bring in
the value 'yes' and same thing, otherwise, still we're going to print the number of asterisks.
Let's see what else - well, this is it for now, actually there will be the nicest way
we print that. So first, we worked printing just the keys, and now we can replace those
keys by actual values. #16: next and last, this is our last quick topic here, typical
languages, you can get out of loops and we have the 'next' command, which will allow
you to skip the rest of the code in a block, if we need a condition so next, will simply
go to the next item in the array and we have last, we can also get out of the foreach loop
entirely, if we meet a certain condition. I'll just go over that quickly, rewrite the
above 'next' with map, and I'll let you go through those on your own. Anything else important
in here, bare blocks, can also, though this is not extremely important, but just be aware
that you can also have a loop in a bare block - what's going on here, we're expecting an
entry and if the magic is 0, this is interesting, yeah, magic, equals zero, this is a condition,
basically, does this equal to zero(?), 'or' if this is not true, than the or will go on
to the next line, which says print "not quite" and we do, and these have to be, now this
is the and, as you've read in the chapter and for an 'and' to work, both have to be
true, but a print command will always return true and then we do, of course, it's just
a command to perform and it's going to redo the block. This is just an interesting way
to do this. More popular, is the 'die' function, that uses the use of the 'or' for die function,
which is a way to get out if something goes wrong. The denominator, as you know, should
never be zero, so a nice way to take care of that is using these lines here - you can
say if the denominator is not equal to zero, if it's true then, it will keep on going but
if it is equal to zero than the or is going to be implemented and the die function is
going to take over and terminate the program. OK, thank you very much, this completes this
lesson :)