Coding 101 22 (Transcript)
Shannon
Morse: Today on
Coding 101 we're getting more Perl with inputs and if then's. Stay tuned.
Netcasts you love from people you trust. This is
TWiT! Bandwidth for Coding 101 is provided by Cachefly at cachefly.com. This
episode of Coding 101 is brought to you by shutterstock.com. With over 35
million high quality stock photos, illustrations, vectors, and video clips,
Shutterstock helps you to take your creative projects to the next level. For
20% off any image file on your new account, go to shutterstock.com and use the
offer code: CODING614. And by Lynda.com. Learn what you want when you want with
access to over 2400 high quality online courses, all for one low monthly price.
To try it free for 7 days, visit lynda.com/c101.
Fr. Robert
Ballecer: Welcome
to Coding 101 it's the TWiT show where we introduce you to the secrets of the
code monkey. I'm Father Robert Ballecer.
Shannon: And I'm Shannon
Morse and for the next thirty minutes, we are going to get you all coded up on
everything you need to know to be a Perl code warrior.
Fr. Robert: Now Shannon last
week we introduced people to Perl and it was more or less, an easy language but
some people kind of freaked out.
Shannon: Yeah, it was
pretty easy. There was a little bit of confusion at the end with the arrays so
today, starting off, since we are recording this right after the last episode
we don't have any user feeds yet so we don't have any codes from our Google+
community so I'm going to show you three of my own codes to get you involved
with variables, scaler variables, and arrays. So let's go ahead and take a look
at my computer. I have three different codes that I have created. These are
called printing scalers and arrays. If I click on printing it just prints a
couple of strings. It says, this is Snubs' first string. Everyone rejoice and
then it has a whole bunch of numbers. So if I go into the actual code- And I'm
just editing this in Notepad. -It basically just says this is the first
variable, its called numbers are cool and it obviously knows it's a number. The
second one is a string called string thing. I put it in quotes and it just
says, my string. Next I printed the string example first and then I printed the
string example first and printed the number example second.
Fr. Robert: Now there are a
few things, thank you for doing this, but there are a few things that are very
important. The first thing is, you'll notice that the capitalization is
important. So you have to match capitalization. If you put capital numbers are
cool, that's different than the variable numbers are cool.
Shannon: So if I capitalize
that c right there....
Fr. Robert: That's not going
to work.
Shannon: I would also have
to capitalize this c right here.
Fr. Robert: Exactly, unless
you do that. So just be aware of that and make sure that you match your
capitalization. The other thing- And people had an issue with this, especially
if you’re using Windows, I know okay. All of the jokes have been done.
Shannon: Hey man I use
Windows and Linux.
Fr. Robert: People are
complaining that it scrolls too fast. Like they don't see the program. That's
why we put the SDI in, the standard input at the bottom. We used to do that in
Python, right? Because what it does is it waits for the user to hit enter. So
if you want, just get used to putting <STDIN> at the bottom and that will
make it to where it won't close the window until you hit enter.
Shannon: Yeah, I just
copied that to every single one of my programming codes for Perl, that made
it easy. So, moving on. I'll go ahead
and go to my second one and this one is scalers. And if I click on that, we
have three different variables that show up: 16, coding 101 is made of win, and
16.5. So we have a float, a regular integer in there, and a string.
Fr. Robert: Okay and what
you're doing is you're illuminating the fact that- And we love this about Perl,
and actually like this about Python too. -We don't have to care about what's
going to go in there. Perl is going to figure it out, right?
Shannon: So let's see for
this first one, the $, I could just call this number. And I'll need to change
this to number as well to make that run. And then the second one is Coding 101,
that's my string, and then with 16.5 you may notice that's not in quotes. That
float number in that one is my variable, and then I just print them. I put the
\n at the ends so that each one enters out into a new line and then standard input
at the bottom so that it enters out. That's it, easy.
Fr. Robert: Nice and easy.
Shannon: And my third one
is the array. So this is built right on top of your code, Padre, that you did
last week.
Fr. Robert: This is the one
that I think some people started to lose it on. It's easy to understand
printing and it's easy to understand simple variables, but this is a little
different.
Shannon: So I'll show you
what my program looks like first and then I'll show you the code. It just says,
here are the values of the array Sailor Moon. So I built an array called Sailor
Moon and I have three different variables in it called; Usagi, Hotaru, and
Setsuna. Three names of Sailor Scouts because that's awesome.
Fr. Robert: The fact that you
know that off of the top of your head is awesome.
Shannon: Shut up.
Fr. Robert: Okay.
Shannon: So the values of
Sailor Moon have also been copied into a new array called, Anime. So here are
the values of the array Anime and since it's just copied straight over, you get
the same thing; Usagi, Hotaru, and Setsuna, so then you hit enter again for the
standard input that I put at the end of each of these different lines of
programming. Now I'm changing the value of the third element. So the third
element is Setsuna in Anime from Setsuna to Sailor Pluto because that's the
Sailor Scout that Setsuna is. So I changed it to Sailor Pluto at the end and
the other two have not been changed because I didn't change those in my array.
Hit enter, and then in the meantime, the array Anime stays the same.
Fr. Robert: Right, exactly.
Because you made a copy of the array. So even though you changed one of them,
the copy stays the same.
Shannon: Aaand there we go.
Fr. Robert: Oh my goodness and
there goes our TV being...
Shannon: Can we watch this
all day?! Yayyy!
Fr. Robert: No we can't.
That's kind of freaky, actually. I forgot that part.
Shannon: Alright, so I'll
hit enter and I'll go ahead and show you the code behind here. Okay so at the
top we have Sailor Moon. So we have this array made with the @ at the beginning
and then I put it in parentheses and each one has quotes around it. The first
one is called Usagi, then we have Hotaru, there's a comma separating each one
followed by a space and then Setsuna is the last one, with a semi-colon at the
end. Next, I printed just a regular string. Here are the values of the array
Sailor Moon and then enter at the end. Then I printed each of the different
variables inside of that array.
Fr. Robert: Now let's pause
here because I think this is something people kind of got confused with.
Because they said, wait a minute.. The array is called Sailor Moon and you used
the @, why are you using the $ down below? This is actually very important
people. If you got confused at the end of the last episode let me explain it
right now. @Sailor Moon, the line we have at the top, absolutely opens up a new
array. And the array, as we learned in the first episode is just a list of
scaler variables. Scaler variables were the first variables we covered, and
that had the $ as their symbol right?
Shannon: First one, second
one, and third one.
Fr. Robert: Exactly. Now at
the bottom you may be wondering, wait a minute. If the array is called Sailor
Moon, why am I using the $, which is associated with the scaler variable? The
reason is because, what is an array? We've already defined what an array was,
an array is a list of scaler variables. So if I'm talking about the entire
array, I use the @. But if I'm talking about an individual element within that
array it's just a scaler variable right? Because an array is nothing but scaler
variables. So in this instance, because I'm calling out index number 0, number
1, and number 2 I use the $.
Shannon: Right so each one
is separated. And then we move down to Anime. So this is where I'm taking those
values of Sailor Moon and I'm copying them into a new array called Anime. So
here we have array number 1 called Sailor Moon. And it's equal to array number
2, which is Anime.
Fr. Robert: Right, and she is
using the array symbol because we're making a copy of the entire array.
Shannon: Yeah, it's just a
copy you're not editing anything or whatever, just copying it over. So if I
print each variable inside of Anime it's going to be the same thing; Usagi,
Hotaru, and Setsuna.
Fr. Robert: Boom, yeah.
Shannon: Moving on. I'm now
changing the value of the third element in Anime from Setsuna, the first name
of it to Sailor Pluto. Here we have Sailor Moon, 2, and that's the very last
variable in our original array right here, Setsuna. So if I scroll back down
now it's going to say equal to and it gives it the new title of Sailor Pluto.
Fr. Robert: You're just
reassigning it. And we've done that before in the languages we've covered.
Shannon: And then I print
out the same thing, I have Sailor Moon; the first variable, the second
variable, and the third variable which is Sailor Pluto. So you see, this one
changed from Setsuna to Sailor Pluto. And then I hit enter again and the last
part of this is, in the meantime the array Anime stays the same. So you'll
notice up here Sailor Moon, 2 changed to Sailor Pluto. Down here we have Anime,
2 does not change. That one stays as Setsuna. Now I've said Setsuna so many
times, I feel like it's not even a word.
Fr. Robert: I know, I know.
Actually, isn't Setsuna like an orange? Like the 11th Doctor liked Setsuna. Oh
no it's a sadsuna...
Shannon: A sad tuna?
Fr. Robert: It was like a
little tangerine... Never mind. Okay so that gives us a really good recap of
what we covered in the first episode.
Shannon: Yes. And I'll be
putting this on GetHub but I haven't gotten around to it yet.
Fr. Robert: Yes, it will be.
Shannon: It will go up
right after this recording.
Fr. Robert: Yeah, folks all
the code from these lessons is going to end up in the GetHub that's linked
inside of the Coding 101 show notes. So make sure to go there, because then you
don't have to copy stuff off of the screen, you can just download the code,
copy and paste it into whatever text editor you're using to create the files-
Shannon: And then break
things.
Fr. Robert: And then break
things. Because we like to break things. You know what doesn't break?
Shannon: What?
Fr. Robert: I was just
thinking about media because sometimes what you want is these durable images or
these video clips or things that make people learn and want stuff.
Shannon: I know of some
media you can get for a really good price.
Fr. Robert: No way.
Shannon: Yeah, way.
Fr. Robert: Okay, hit me.
Shannon: So it's a site
called shutterstock.com and they have over 35 million high-quality photos and
videos for everything that you could ever need. So obviously, this episode of
Coding 101 is brought to you by shutterstock.com. At shutterstock.com you'll
find perfect images or videos for your next creative project. So whether it's a
website or you need to post some stuff on your blog, a video or any other type
of project that you're working on, you can choose from over 35 million stock
photos, illustrations, vectors, and video clips. Shutterstock sources images
from around the world and they put them at your fingertips from tons of
contributors, who are professional photographers and artists. And plus, they
review every single image that comes in, every single video that comes in for
content and quality before adding it to the library so you're not going to get
anything that's really really terrible in this site. Everything is awesome. So they
add 20,000 images every day, and so every time you visit you're going to find
something new. Whether you're just searching for flowers every time or code
every time, you're going to find something new. And they have flexible pricing.
You can choose individual image packs or a monthly subscription for the best
deal. You can download 25 images per day with a standard subscription, that's
pretty awesome. And you can download any image in any size while paying only
one low price. Bam! Shutterstock also gives you the images you need to get your
creative project to the next level and they make it easy. They have
sophisticated search tools that allow you to search anything from color, file
type, to gender, to emotion. Sharable light boxes so you can save images to a
light box gallery where you can access them and you could even share them with
other team members or your friends and say, oh do you like this picture? And an
amazing, award-winning iPad app so you can search on the go and use it to
display images during a presentation. Wonderful, hey you just got a raise.
Shutterstock has a global market place on top of all of that. Multi-lingual
customer support in more than a dozen international countries and full time
customer support throughout the week so if you find you're needing help in the
middle of the night, don't worry they've got you covered. So today I found a
few pretty interesting ones of Corgi's, of course because Brian's in charge
over there and he loves Corgi's but they're so cute. Can we find happy Corgi's?
Fr. Robert: They're all happy,
there's not such a thing as a sad Corgi.
Shannon: Sad pandas... Oh
find a panda, I want a panda picture! Give me a panda. Yes, pandas.
Fr. Robert: No, even better...
Give me a red panda. Now those things are cute.
Shannon: Red pandas are
adorable. Yes! That just made my day. There's no credit card required so you
can try Shutterstock today by signing up for a free account. No credit card
needed, just start an account and begin using Shutterstock to help imagine what
you're next project could be like and save favorite images to a light box to
review later. Once you decide to purchase, use the offer code: CODING614 and
new accounts will receive 20% off any image file. Any, go choose, find one.
You'll spend so many hours on there, trust me there's so many good pictures to
look at. That's shutterstock.com and for 20% off any image file on new
accounts, use the offer code: CODING614. And of course, we thank Shutterstock
for supporting our show, Coding 101. Thank you, Shutterstock. We like your
pictures.
Fr. Robert: Thank you
Shutterstock. We love you and the red pandas. Thank you for the red pandas, and
the Corgi's.
Shannon: I wish someone
would send us a red panda.
Fr. Robert: They tear things
apart...
Shannon: That's okay, just
leave it in the studio it'll be fine. Okay moving on.
Fr. Robert: Moving on, let's
talk a little bit about coding. Now, we of course are pre-taping this episode
because she's got to get married. Evidently, that's more important than coding.
Shannon: That's what
happens.
Fr. Robert: Whatever, I don't
know.
Shannon: I'm old.
Fr. Robert: But I jumped into
the old brain time machine and I know there's a lot of people screaming at us
right now in both email and the G+ group saying, Padre well how about this? Why
didn't you do it this way? Yes, there are so many ways to do everything in
Perl. In fact, that's one of the things that our code warrior tried to instill
in me when he was running me through some of the code that we would be running.
We are giving you a single way and normally we're giving you the easiest way to
do something. There are much more elegant ways to do pretty much every
function, every process, every procedure that we're going to teach you. That's
one of the wonderful things about Perl, which is, you choose the way that you
like and choose the way that works best for the problem you're trying to solve.
Shannon: It's very similar
to using the Linux terminal in a way.
Fr. Robert: It really is,
right? There's no bad way to use it, if you can get something done then that's
great and the more ways you learn how to do it, the better off you'll be. So
we're teaching you one way but please- And we're both going to say this. -Don't
stop at Coding 101 because we're never going to be able to give you in the 8
weeks of any programming module, everything that you need for language, right?
Shannon: Right. And of
course, if you know an easier way to do something or if you feel like the way
that you have been doing for years is easier, then share it with us on our
Google+ community.
Fr. Robert: And actually, the
wonderful thing is when you comment, everyone is learning. It doesn't matter
how advanced you are, you can always be introduced to something new. So be
nice, be friendly and tell someone, hey I noticed you've been doing it this way
but have you considered doing this? It's one of the things we do to help out
our code monkeys.
Shannon: So we've reviewed
arrays, we reviewed scalers and variables so what's next?
Fr. Robert: Well variables are
great because they allow us to- Well you can't actually do anything in a
program without them but there's one thing that's really missing. It's a very
basic feature of any programming language.
Shannon: Let me guess.
Fr. Robert: Go ahead.
Shannon: Allowing a user to
put in their own input?
Fr. Robert: Yeah, because if
the only input we can use is hard coded into the software then it's not really
useful so we wanted to give you one of the easiest ways- Again there are many
ways to do this. - But one of the easiest ways to get input from the user into
the program and you've actually seen it before because I've put it at the
bottom of my programs in order to keep the window...
Shannon: Are you talking
about Standard Input?
Fr. Robert: <STDIN> This
is a function that is built into Perl that allows you to wait for input from
the user and when they hit enter, it takes that input and puts it into the
variable that you assign. There are several ways to do this but whenever you
use STDIN it's also going to include the carriage return. So if you're a really
good coder, I'm not going to cover it here, but if you're a really good coder
go ahead and look up 'chomp.' The chomp function that's built into Perl, what
it does is it strips off the carriage return.
Shannon: It chomps it.
Fr. Robert: It chomps it. But
okay let's take a look at what some of this code actually looks like. It's
really really simple. This is what a Standard Input looks like. This program is
going to print a line saying, please type in your age. We've done this before
in C# and in Python and then it's going to create a variable and it's going to
say, age is equal to Standard Input <STDIN>. Again, all it does is it
just waits for the user to type in something and when they hit the carriage
return, Standard Input is going to put that value into the variable called,
age. Then it's going to print, your age is, and then the variable.
Shannon: And then whatever
they type in.
Fr. Robert: It's just going to
put out whatever they put in. Super simple program and when I run it, it's
going to look a little something something like this... Please type in your
age:
Shannon: 62.
Fr. Robert: I am 82.
Shannon: I was close.
Fr. Robert: Yeah. Your age is
82. Super simple, but again, this is important because it allows us to take
information from the user and actually run it through our program. The other
thing to remember here- And I'm going to stress this a couple of times until
people get it into their memory. -Is I don't have to end the print string in
order to print the variable. This kind of freaked out some people because
they're used to seeing Python and C# where you'd have to do something like
this, you'd have to say okay, end the string and then print the variable. You
don't have to do that. Because in Python, it's looking for this and it knows
when you put that there, it knows you're dealing with a scaler variable and it
will print a scaler variable.
Shannon: That's cool, I got
this so far.
Fr. Robert: Let's go from
that, it's Standard Input and it's really simple. Let's go to something that is
a little less simple, but we've played with it a lot and that is going to be
if, then, else. So this is going to be using comparative operators. Do you
remember these from C# and from Python?
Shannon: Yeah.
Fr. Robert: Equal to, greater
than, less than, equal to yeah. So these are the six basic operators that I can
use to put inside of a conditional statement. Remember a conditional statement
is either true or false. There's only two options, either true or false. So for
example, if I have 6=6, that statement is true. If I have 5>7 and I put that
into a conditional statement that will be false because 5 will never be greater
than 7. Now I can use these conditional operators to compare two variables. And
in those two variables, it will allow me to change the condition from true to
false and to either have an if statement run or not. And if statement run or
not, have a piece of code run or not. Again, this is the basic building block
of any program. The ability to make a decision is based on if, then, else
statements. Okay so let's take a look at what the code looks like, here we go.
This is actually a revamp in the comments from a Python program that we did a
couple of weeks ago that took number A and number B and compared number A to
number B and then it just said, is A less than B, is A equal to B, or is B less
than A?
Shannon: Yeah.
Fr. Robert: Very simple and
this is a decision tree. So let's say I entered 1 for A and 2 for B. The first
if would say, Is 1 less than B? It's going to say yes and it will print A<B
and it's going to skip everything else.
Shannon: But if you switch
that around...
Fr. Robert: Right, then it
would get to the first statement and say, is 2<1? And it would say, no it's
not. Then it would say, then is 2=1? It's not so then it would say, if it's not
the first statement, it's not the second statement, it has to be the third
statement. And therefor, B < A. And the nice thing about Perl is that it's
exactly the same, although the terms are slightly different. In Python I used
if, L-if, and else. In Perl I'm going to use if, else-if, and else. So I change
one thing and everything else looks. Now here's my thing, I'm going to say,
please enter a first number a whole number. Input is equal to Standard Input.
Please enter a second whole number. Input two is equal to Standard Input, so I
now have two numbers from the user, right? Then it's going to run me through
this, now my if, else statements look almost like Python. You're going to
notice a few differences and the first is you're going to notice I'm using
brackets again. So we're back to C#.
Shannon: Ah, back to
brackets okay.
Fr. Robert: Notice that I have
a semi colon in the code base for what it would execute. So if this conditional
statement is true, if input one is less than input two, then print, the first
number is less than the second number. If it's not true then it's going to move
onto the second case. Well is input 2 greater than input 2? If it is then it's
going to say that the first number is greater than the second number. If it's
not greater than or less than, the only thing left is that it's equal. So when
I run this, it's going to do this: My software will say please enter a whole
number, I'm going to say 6. Please enter a second whole number and I'll say 6.
It's going to run through the if and through the else if, get down to the last
one and say, oh well they're the same value.
Shannon: Yeah, okay that's
easy.
Fr. Robert: Very simple
decision tree. Very simple, but this is how it works in Perl. So if you
remember your Python examples, it's the exact same. The only thing that's
different is we call the else statement different and we put the code base in brackets.
Shannon: I've got it.
Fr. Robert: Nice and simple.
Now the cool thing about that, again, is because really it doesn't matter how
advanced your program gets, you're going to be building if else statements into
your software.
Shannon: Yes, pretty much
no matter what you're doing.
Fr. Robert: Exactly. There's
something else I should mention and actually, our code warrior is going to get
to it but that is, I used 6 conditional operators. I used equal to, not equal
to, greater than, less than, greater than equal to, or less than equal to.
There's actually a whole lot more.
Shannon: There are?
Fr. Robert: And actually Perl
is pretty extensive in the number of conditional operators you have. But they
all work the same way, all conditional operators are there either to give you a
true or a false. To either allow a tree to run or not to run.
Shannon: Where can I find
all of these?
Fr. Robert: We're going to
have them in the notes inside the show notes. We're also going to post a a
couple of links to some decent Perl documents. But more or less, just play with
them. If you have a conditional statement, you can put anything in there so
play around with some of the more advanced conditional operators-
Shannon: I can't wait to
write my first if else statement in Perl.
Fr. Robert: Absolutely. We are
talking a lot about knowledge that people should have that maybe don't have
right now. And they're not just going to get it all from a 101 program.
Shannon: I bet there's a
place online where you could find something to help you get that.
Fr. Robert: I wish there were.
Some place that could give you easy to understand, easy to follow, easy to skip
to lessons so you could find exactly what you're looking for.
Shannon: I know a site.
Fr. Robert: What?
Shannon: Lynda.com.
Fr. Robert: I've heard of this
lynda.com. In fact, what I've heard about lynda.com is that it is the place,
the online repository of knowledge. It's a place you go if you want to update
your business skills or perhaps learn a new programming language. Or maybe even
recall something that you've forgotten, that's what lynda.com is all about.
Lynda.com helps you to learn and keep up to date with your software, pick up
brand new skills or explore new hobbies with their easy to follow video
tutorials. Whether you want to master the fundamentals of programming, learn a
new programming language like Python, or build your first iOS app with Swift,
lynda.com offers thousands of courses and a variety of topics. On lynda.com,
exploring programming fundamentals, or getting started in everything from
databases and object oriented design to code efficiency and test driven
development is part of the package. There's also a course on the new features
of Java SE8 and the foundations of programming, programming for kids which teaches
kids to program using techniques, apps, and hardware. In other words, lynda.com
is all about giving you the methods and tools that you need to learn in the way
that will be most efficient. We've been using lynda here on Coding 101 to sort
of fill in the knowledge. Especially since a lot of these languages- Well it's
been years maybe decades since I've actually done them in the real world, it's
always nice to have an easy to follow, easy to access, easily indexed
repository of knowledge. Lynda has over 2400 courses with more added weekly.
All lynda.com courses are produced at the highest quality. Not like homemade
videos on YouTube, which are great but they may not have the best features to
go along with the information. Lynda.com works with software companies to
provide you updated training the same day new versions hit the market so you'll
always have the very latest skills. At lynda.com the instructors are
accomplished professionals at the top of their fields and they're passionate
about teaching. These are not in a classroom 24 hours a day type folks, they're
actually out in the field, they know what works, and they know what the
business world wants. Lynda has courses for all experience levels, for
beginners, intermediate, advanced and you can watch from your computer, tablet
or your mobile device. Whether you have 15 minutes or 15 hours, each course is
structured so that you can learn from start to finish. You can also search the
transcripts to find answers or read along with the video. They even offer
certificates of completion so that when you finish a course, which you
obviously will, you can publish it to your LinkedIn profile. Which is fantastic
if you're a professional looking to tell companies what you've learned. So
here's what we want you to do. We want you to try Lynda, it's only $25/month
for access to the entire lynda.com course library, or for $37.50/month you can
subscribe to the premium plan which includes exercise files that let you follow
along with the instructor's projects using the exact same project assets that
they do. You can try lynda.com right now with a free 7 day trial. Visit
lynda.com/c101 to access the entire library. That's over 2,400 courses free for
7 days. That's lynda.com/c101 and we thank lynda for their support of Coding
101. Snubs found donuts.
Shannon: Somebody just left
these here and they're mine now.
Fr. Robert: That was weird,
that was like input- This is standard input actually.
Shannon: I don't know how
they got here, I think it was Liz. Liz gave them to us.
Fr. Robert: Aside from the
coolness of donuts just showing up here on the set, I think that this is the
time Snubs where we talk to our code warrior.
Shannon: Yay!
Fr. Robert: Ladies and
gentlemen, without further adieu, Mr. Patrick Delahanty. Perl father.
Patrick
Delahanty: I had two
donuts already.
Fr. Robert: I think it really
is donut day.
Shannon: Thank you for
saving me one.
Fr. Robert: Patrick, we did
some basic stuff today. We talked about Standard Input, we talked about
decision trees with if else statements and that's kind of fundamental right? No
matter the language, you kind of have to know that stuff.
Patrick: Yeah, and so I'm
going to demonstrate a little bit more, take it a step further.
Shannon: Ready.
Patrick: Okay I've got a
program here, I'm going to run it first and then I'll show you how it worked.
Pick a number any number.
Shannon: 16
Patrick: 16. You entered an
even number, let's try this again. Pick a number, any number.
Fr. Robert: 4,001!
Patrick: 4,001! You entered
an odd number.
Shannon: It works!
Patrick: This also works
with negative numbers. -52 and 52 is an even number.
Fr. Robert: It also works with
floating points.
Patrick: I haven't tried
that.
Shannon: Let's try it.
Fr. Robert: 3.14.
Patrick: 3.14.
Shannon: Odd number, yeah
it actually did work.
Patrick: So now let's look
at the code behind this. Here we see I've got the print statement and I'm
accepting Standard Input, it's similar to what you've had in other programs.
And here, I've got my if statement. If number-
Fr. Robert: Mod.
Patrick: This is percent,
so it's a mod. So if number mod 2 and 2- Because I want to determine even or
odd.
Fr. Robert: For the folks who
may not remember, we did this in Python. What mod does is it doesn't give you
the quotient, what it does is it gives you the remainder. So if I do something
mod 2, it's saying if I divide it by 2, what's left over. Well if it's an even
number there should be zero left over but if you mod 2 an odd number, obviously
you're going to get at least 1.
Shannon: Now isn't mod
short for modulo?
Fr. Robert: Modulous. Which is
cling-on for, it's an art.
Patrick: Modulous will be
in the next Avengers movie I think.
Fr. Robert: Okay, focus
people! Focus, back to the code. This is actually a fantastic example of a
decision tree, so you're using simple math which is the modulous and that
allows us to determine whether an inputted number is going to be odd or even.
But I notice that your if then statement looked a little different than mine.
Patrick: You mean just the
formatting?
Fr. Robert: Yeah.
Patrick: My habit is to put
the brackets on the same line rather than a new line.
Fr. Robert: Exactly but this
is one of the cool things remember, it doesn't matter. If you go back to his
code, you'll see that as long as you've got the semi colon where it should be,
this again, could be all on the same line.
Shannon: And as long as you
have your open and close brackets it works.
Fr. Robert: Alright, what else
do you have for us?
Patrick: We'll go into my
next little bit and on this one I am again, doing Standard Input, but here I'm
evaluating a string. And so it's asking, what is your favorite animal? And then
if animal equals bunny- And you'll notice here I have the \n because it's
included in the Standard Input after I hit return.
Fr. Robert: And this is not
one of our standard six operators that we talked about so this is EQ. EQ is
interesting because what it does is it compares a string. So is one string
equal to another string.
Patrick: Yes, so then it
says it repeats the input and you said whatever, it says, yes bunnies are the
best. Or it said, you said animal(whatever the animal is) and it says, alright
whatever. So let's try running this real quick. What's your favorite animal?
Let's put in douge. Let's try it again with the best animal, bunny. You said
bunny, yes bunnies are the best.
Fr. Robert: Sometimes I feel
like this game is fixed.
Patrick: Now if I entered
like, rabbit. You said rabbit, okay whatever. But it's like the same thing so
I've got another example here. And this one, I'm accepting two values, so if
animal = bunny-
Fr. Robert: Tell them the
pipes, what do the pipes mean?
Patrick: The two pipes mean
or. Also, if the code needed it I could put &. Somehow if they entered
bunny&rabbit, which they can't really do in this case.
Fr. Robert: Right. Now one of
the things that people should realize is that when we start doing this,
especially the pipes and the ampersands, you can do multiples so you could say
if this or this or this or this and just keep going. It gets really messy
really fast. Or you could say if this or this and that or that and this- So
these are just logical operators but folks be very careful when you start doing
that because it's incredibly easy to trip yourself up.
Patrick: Yeah, and you'll
see how I used parentheses to try to keep track here. I've got the bunny in one
set of parentheses and the rabbit in the other with another set of parentheses
around the whole thing.
Fr. Robert: Right.
Patrick: So we'll run this
real quick. Bunny, yes bunnies are the best. Rabbit, yes bunnies are the best.
But if I put in bunny rabbit, alright whatever. So one option to get around
this is I could keep entering more or's-
Fr. Robert: That would be
ridiculous.
Patrick: But there's other
ways to do this so what I'm going to introduce here is a regular expression and
this is what Perl is best at. It's amazing.
Fr. Robert: I have not found another
language that does regular expressions better than Perl.
Patrick: And so we're going
to use a lot of this going forward. What I've done here is it's all the same
script except I changed this.
Shannon: What is that?
Patrick: Okay so what this
is, that equals tilda. It's looking for- And then I have the slash bunny slash-
Shannon: What in the world?
Patrick: So what this is
doing is it's actually looking inside the string for bunny.
Shannon: So they could have
typed in a whole bunch of-
Patrick: Yeah they could
have typed in, I really hate that bunny. And it would say, oh you typed bunny.
Fr. Robert: Right so what
you're saying is, if anything within the entire string, it finds bunny. It
finds that collection of characters and it doesn't matter if it's surrounded by
a whole bunch of other characters, it will take it as a valid response.
Patrick: Yes and the G here
means global so it looks in the entire string and the I is case insensitive. So
if I type it in all caps, it's going to match. And I did the same thing over
here for rabbit so we'll exit out and run this. Favorite animal, let's just do
the standard bunny. Yes, that works. We'll do bunny rabbit and yes bunny
rabbit. Let's do, I really think that rabbits are awesome.
Fr. Robert: There you go.
Patrick: Yeah, it finds it
in there and so it knows that-
Shannon: Oh my gosh Brian.
Fr. Robert: I am bunny
overloaded right now Brian.
Shannon: Is that the
bunnies noise when it types? Bunny programming.
Patrick: Bunnies don't type
very well.
Fr. Robert: What I love about
this, if you could go back to your code, what I love about this is if we had
done this the old way in Python, we would be doing every possible combination
in order to make sure that we get a valid response. This is what makes regular
expressions in Perl so powerful, which is, if I'm just looking for a string, I
can tell Perl as long as this string of characters comes up I can consider this
a valid response. That's incredibly powerful.
Patrick: Yeah, and this
comes in handy like if someone were to enter in an email address and you want
to verify if it's valid. Does it contain an @ and at least one period-
Fr. Robert: And a . and a com.
Patrick: Exactly, or like a
certain number of letters in there and so I love Perl for this.
Fr. Robert: Fantastic.
Shannon: Do we have time
for the last code?
Fr. Robert: I think so.
Patrick: So I get a little
crazy here and in this one it says enter your favorite number and then you see
number.
Shannon: What in the world
is going on? I give up, I quit.
Patrick: There's a whole
list of different attributes- Well I don't know if you'd call them attributes,
but different things you can put in here to determine if it's in the string. So
what I'm doing here is saying, if number contains, and then this \d. That means
not a digit.
Fr. Robert: Okay.
Patrick: \d tells me it's
not a number, the + here means there's at least one of them and then because
the Standard Input has the \n, I work around that by saying okay it contains
any number of other things, maybe none, and then the \n. And the $ is the end
of the string.
Fr. Robert: Right. So this is
actually a quick and dirty way to do a sanitization so if you are expecting a
number you can run this one line and it's using a regular expression to parse
the input and it will say whether or not it actually contains a number.
Shannon: That's cool.
Fr. Robert: There you go.
Patrick: But if I run it
again it'll say, thank you!
Fr. Robert: Again, this is
stuff we've done in Python and it looks crazy confusing, but what we're going
to have in the show notes-
Shannon: I'm going to have
a hard time memorizing that.
Fr. Robert: Don't memorize it,
it will eventually come. -We're going to put all of those expression in the
show notes. We don't have examples for them all because there are a lot of them
but what you can do is take the expressions, the if then statements and start
substituting them for those regular expressions. Get in there and see what each
and every one of them does. Folks, that's really the best way to learn the
code. Patrick, thank you so very much for sharing that with us.
Patrick: It's my pleasure.
Fr. Robert: Could you give me
a hint about what the Coding 101 monkeys are going to get for next week?
Patrick: Next week I think
we're going to hit those regular expressions again and search and replace.
Fr. Robert: Okay and I
believe, is it in two weeks that we're going to actually start bringing in
stuff from the web?
Patrick: We might.
Shannon: That's going to be
fun.
Fr. Robert: That's going to
scare people.
Patrick: Oh it's so fun,
it'll be great. Patrick if you could, thank you again for being our code
warrior, but if you could tell the folks at home where they can find you like
your Twitter address or your website, where should they find out more about
you?
Patrick: On Twitter, I'm
@PDelahanty and my website, enemycodes.com is coded entirely in Perl. Now if you want to find out anything about
anything what we talked about today, you've got to go to our show page. There
you'll find the episodes and also our GIT hub, right?
Shannon: That's right we
put all of our code in our GIT hub and we also separate them out by modules so
you can easily find every single piece of code that we show in our episodes.
We're also on iTunes if you use iTunes for all of your RSS feeds, or podcasts,
definitely subscribe to us over there and share it with your friends if you think
they would be interested.
Fr. Robert: That's right and
if you want to get more involved, as we mentioned at the beginning of the show
you've got to join our G+ group. Drop in at g+.to/twitcoding. You're going to
find a vibrant community, over 1,000 people who are at all levels of
programming expertise from the noobs to the super, super gurus. So if you've
got a question, you want to answer some questions or, if you want to show
Shannon the code that you wrote that you think we should show off, that's where
you go.
Shannon: Definitely and
I'll be sharing my own code on the episodes as well. Of course, if you aren't
on Google+ and you're like, oh I don't like social networks I'm on Twitter.
We're also on Twitter and we're on there pretty much constantly so you can find
me @snubs.
Fr. Robert: And I'm @padresj.
I'd say until next time I'm Father Robert Ballacer.
Shannon: I'm Shannon Morse.
End of file.
Fr. Robert: End of line.