17
Perl Perl Day 3 Day 3

Perl Day 3. 1 + 1 = 2 We’ve previously seen you can do math on variables We’ve previously seen you can do math on variables$Num=1$Num2=$Num+7;print(“$Num2\n”);

Embed Size (px)

Citation preview

Page 1: Perl Day 3. 1 + 1 = 2 We’ve previously seen you can do math on variables We’ve previously seen you can do math on variables$Num=1$Num2=$Num+7;print(“$Num2\n”);

PerlPerl

Day 3Day 3

Page 2: Perl Day 3. 1 + 1 = 2 We’ve previously seen you can do math on variables We’ve previously seen you can do math on variables$Num=1$Num2=$Num+7;print(“$Num2\n”);

1 + 1 = 21 + 1 = 2 We’ve previously seen you can do math on variablesWe’ve previously seen you can do math on variables

$Num=1$Num=1$Num2=$Num+7;$Num2=$Num+7;print(“$Num2\n”);print(“$Num2\n”);

You can even do math on the same variable:You can even do math on the same variable:$Num1=$Num1+7;$Num1=$Num1+7;

A commonly used short cut for adding 1 to a number is:A commonly used short cut for adding 1 to a number is:$Num1++;$Num1++;

Likewise you can subtract one:Likewise you can subtract one:$Num1--;$Num1--;

Another shortcut allows you to add a number to a variable (This Another shortcut allows you to add a number to a variable (This is the old way to do that: $Num1=$Num1+7:)is the old way to do that: $Num1=$Num1+7:)$Num1+=7;$Num1+=7;

That works for more than addition, you can multiply, subtract or That works for more than addition, you can multiply, subtract or divide.divide.$Num1*=2;$Num1*=2;$Num1/=5;$Num1/=5;

Page 3: Perl Day 3. 1 + 1 = 2 We’ve previously seen you can do math on variables We’ve previously seen you can do math on variables$Num=1$Num2=$Num+7;print(“$Num2\n”);

Nap time!Nap time!

Computers go very fast, a 2.8GHz Computers go very fast, a 2.8GHz processor can do 2.8 billion instructions processor can do 2.8 billion instructions per second. Many of our computers in the per second. Many of our computers in the datacenter have 8 cores at 2.8Ghz, datacenter have 8 cores at 2.8Ghz, meaning they can do well over 20 billion meaning they can do well over 20 billion instructions per second.instructions per second.

Sometimes in the real world we’d like to go Sometimes in the real world we’d like to go a little slower, so we can ask the computer a little slower, so we can ask the computer to take a nap while it waits for us:to take a nap while it waits for us:sleep(10);sleep(10);

Page 4: Perl Day 3. 1 + 1 = 2 We’ve previously seen you can do math on variables We’ve previously seen you can do math on variables$Num=1$Num2=$Num+7;print(“$Num2\n”);

Asking the user questionsAsking the user questions

What if you want your script to ask the What if you want your script to ask the user a questionuser a question– There is a magic “file” which corresponds to There is a magic “file” which corresponds to

the keyboard, called STDIN (standard input).the keyboard, called STDIN (standard input).– It is already open, you don’t have to open, or It is already open, you don’t have to open, or

close it like a file, you can simply use it.close it like a file, you can simply use it.– If you assign <STDIN> to a variable the If you assign <STDIN> to a variable the

program will stop and wait for the user to enter program will stop and wait for the user to enter 1 line.1 line.

print(“Enter a number\n”);print(“Enter a number\n”);$Num=<STDIN>;$Num=<STDIN>;print(“Thanks you gave me $Num\n”);print(“Thanks you gave me $Num\n”);

Page 5: Perl Day 3. 1 + 1 = 2 We’ve previously seen you can do math on variables We’ve previously seen you can do math on variables$Num=1$Num2=$Num+7;print(“$Num2\n”);

Gobble gobble…chomp Gobble gobble…chomp chompchomp

Most lines you read in from a file, or Most lines you read in from a file, or keyboard, ended with a \nkeyboard, ended with a \n– Usually in a script you don’t want the \n Usually in a script you don’t want the \n

on the end of the string already, because on the end of the string already, because you are going to print(“$string\n”);you are going to print(“$string\n”);

– chomp safely removes any \n’s from the chomp safely removes any \n’s from the end:end:

$Answer=<STDIN>;$Answer=<STDIN>;

chomp($Answer);chomp($Answer);

Page 6: Perl Day 3. 1 + 1 = 2 We’ve previously seen you can do math on variables We’ve previously seen you can do math on variables$Num=1$Num2=$Num+7;print(“$Num2\n”);

Picking a Random NumberPicking a Random Number

The perl function rand() picks a number The perl function rand() picks a number between 0 and 1.between 0 and 1.

If you want a random number between 1 If you want a random number between 1 and 100, you’d do:and 100, you’d do:$RandNum=int(rand()*100);$RandNum=int(rand()*100);

This says pick a number between 0 and 1. This says pick a number between 0 and 1. Multiply that result by 100 (which gives Multiply that result by 100 (which gives you something like 54.242482. We only you something like 54.242482. We only care about the 54, which “int” returns only care about the 54, which “int” returns only the integer partthe integer part

Page 7: Perl Day 3. 1 + 1 = 2 We’ve previously seen you can do math on variables We’ve previously seen you can do math on variables$Num=1$Num2=$Num+7;print(“$Num2\n”);

Push it real good…Push it real good…

On day 1 we mentioned arrays On day 1 we mentioned arrays

@Days=(‘mon’,’tue’,’wed’,’thu’,’fri’);@Days=(‘mon’,’tue’,’wed’,’thu’,’fri’); What if we now wanted to add the What if we now wanted to add the

weekend? The array is already weekend? The array is already defined.defined.

push(@Days,'sat');push(@Days,'sat'); Now, lets assume Sun should be first:Now, lets assume Sun should be first:

Unshift(@Days,’sun’);Unshift(@Days,’sun’);

Page 8: Perl Day 3. 1 + 1 = 2 We’ve previously seen you can do math on variables We’ve previously seen you can do math on variables$Num=1$Num2=$Num+7;print(“$Num2\n”);

Pop goes the weaselPop goes the weasel

Well if we have push, we must have Well if we have push, we must have pop:pop:

$LastDay=pop(@Days);$LastDay=pop(@Days); Note pop pulls from the end of the Note pop pulls from the end of the

arrayarray Shift pulls from the frontShift pulls from the front

$FirstDay=shift(@Days);$FirstDay=shift(@Days);

Page 9: Perl Day 3. 1 + 1 = 2 We’ve previously seen you can do math on variables We’ve previously seen you can do math on variables$Num=1$Num2=$Num+7;print(“$Num2\n”);

Ahh…I’m getting loopyAhh…I’m getting loopy

Sometimes you want to do something Sometimes you want to do something more than once.more than once.

Sometimes you want to do something Sometimes you want to do something more than oncemore than once

This is called a loop. There are 4 This is called a loop. There are 4 types of loops, usually you can do the types of loops, usually you can do the same job with all 4, it’s just some are same job with all 4, it’s just some are easier for one situation or another.easier for one situation or another.

The first 3 are almost identicalThe first 3 are almost identical

Page 10: Perl Day 3. 1 + 1 = 2 We’ve previously seen you can do math on variables We’ve previously seen you can do math on variables$Num=1$Num2=$Num+7;print(“$Num2\n”);

WhileWhile A while loop continue to do whatever is A while loop continue to do whatever is

between the {}’s until the Test part is between the {}’s until the Test part is false:false:

while(test) { … }while(test) { … } This will print the numbers 10, 9, 8…1This will print the numbers 10, 9, 8…1$i=10;$i=10;while($i>0)while($i>0){{ print("$i\n");print("$i\n"); $i--;$i--;}}

Page 11: Perl Day 3. 1 + 1 = 2 We’ve previously seen you can do math on variables We’ve previously seen you can do math on variables$Num=1$Num2=$Num+7;print(“$Num2\n”);

DoDo

A do loop is almost identical to a while A do loop is almost identical to a while loop, it’s just you test at the end instead of loop, it’s just you test at the end instead of the start:the start:

$i=10;$i=10;

dodo

{{

print("$i\n");print("$i\n");

$i--;$i--;

} until ($i<0)} until ($i<0)

Page 12: Perl Day 3. 1 + 1 = 2 We’ve previously seen you can do math on variables We’ve previously seen you can do math on variables$Num=1$Num2=$Num+7;print(“$Num2\n”);

ForFor A for loop is most commonly used when you want A for loop is most commonly used when you want

to just do something a set number of times (like to just do something a set number of times (like the last example):the last example):

As you will see it’s much shorterAs you will see it’s much shorter The for part has 3 sections separated by ;The for part has 3 sections separated by ;

– The first part is the initialization, $i=10The first part is the initialization, $i=10– The second part is the test it runs each time through the The second part is the test it runs each time through the

loop, it keeps going so long as it’s true $i>0loop, it keeps going so long as it’s true $i>0– The 3The 3rdrd part is what you’d like to do to $i each time part is what you’d like to do to $i each time

through: $i--through: $i--

for($i=10;$i>0;$i--)for($i=10;$i>0;$i--){{ print("$i\n");print("$i\n");}}

Page 13: Perl Day 3. 1 + 1 = 2 We’ve previously seen you can do math on variables We’ve previously seen you can do math on variables$Num=1$Num2=$Num+7;print(“$Num2\n”);

foreachforeach Foreach is used when you have an array of things you want Foreach is used when you have an array of things you want

to do something to each:to do something to each:@Colors=(‘red’,’green’,’blue’,’yellow’);@Colors=(‘red’,’green’,’blue’,’yellow’);foreach $Color (@Colors)foreach $Color (@Colors){{ print(“Do you like $Color [y/n]?\n”);print(“Do you like $Color [y/n]?\n”); $Answer=<STDIN>;$Answer=<STDIN>; chomp($Answer)chomp($Answer) if($Answer eq ‘y’)if($Answer eq ‘y’) {{ print(“OK, you like $Color\n”);print(“OK, you like $Color\n”); }}}}

Page 14: Perl Day 3. 1 + 1 = 2 We’ve previously seen you can do math on variables We’ve previously seen you can do math on variables$Num=1$Num2=$Num+7;print(“$Num2\n”);

Nested LoopsNested Loops

Sometimes you have 2 different Sometimes you have 2 different things you need to go over.things you need to go over.– Think of a deck of cards.Think of a deck of cards.

There are 4 suites (spades, hearts, dimonds, clubs)There are 4 suites (spades, hearts, dimonds, clubs) There are 13 cards in each suite (A,2,3…9,10,J,Q,K)There are 13 cards in each suite (A,2,3…9,10,J,Q,K)

– Lets play a game of guess the card.Lets play a game of guess the card.

Page 15: Perl Day 3. 1 + 1 = 2 We’ve previously seen you can do math on variables We’ve previously seen you can do math on variables$Num=1$Num2=$Num+7;print(“$Num2\n”);

Picking the winning card.Picking the winning card. We’ll need to number all the cards, and then pick a random We’ll need to number all the cards, and then pick a random

number between 1 and 52 to pick a card.number between 1 and 52 to pick a card. Remember rand()…it’ll help you pick a numberRemember rand()…it’ll help you pick a number To number the cards, you’ll need a loop inside a loop. To number the cards, you’ll need a loop inside a loop.

– The outer loop will go across all the suitesThe outer loop will go across all the suites– The inner loop will go across all the cardsThe inner loop will go across all the cards– The 2The 2ndnd loop must be inside the first, because there is an A in all 4 loop must be inside the first, because there is an A in all 4

suites.suites.$CardNumber=int(rand()*52);$CardNumber=int(rand()*52);

$CurrentNum=0;$CurrentNum=0;foreach $Suite (@Suites)foreach $Suite (@Suites){{ foreach $Card (@Cards)foreach $Card (@Cards) {{ $CurrentNum++;$CurrentNum++; if($CurrentNum==$CardNumber)if($CurrentNum==$CardNumber) { $PickedCard="$Card of $Suite"; }{ $PickedCard="$Card of $Suite"; } }}}}

Page 16: Perl Day 3. 1 + 1 = 2 We’ve previously seen you can do math on variables We’ve previously seen you can do math on variables$Num=1$Num2=$Num+7;print(“$Num2\n”);

Now let the user guessNow let the user guess Lets use a do loop until they succeed or give upLets use a do loop until they succeed or give up We’ll start off assuming they aren’t done We’ll start off assuming they aren’t done

($done=0), then if they get it right, or quit we’ll ($done=0), then if they get it right, or quit we’ll set done to 1, so our “until” will be $done=1;set done to 1, so our “until” will be $done=1;– Inside this loop we’ll prompt them to enter a guessInside this loop we’ll prompt them to enter a guess– Read their answer, chomp itRead their answer, chomp it– Check if it’s rightCheck if it’s right

Tell them they are rightTell them they are right Set $done=1Set $done=1

– Check if they gave upCheck if they gave up Set $done=1Set $done=1

Finally tell them what the random card was, just Finally tell them what the random card was, just so you know it really works.so you know it really works.

Page 17: Perl Day 3. 1 + 1 = 2 We’ve previously seen you can do math on variables We’ve previously seen you can do math on variables$Num=1$Num2=$Num+7;print(“$Num2\n”);

#!/usr/bin/perl#!/usr/bin/perl

@Suites=('Spades','Hearts','Dimonds','Clubs');@Suites=('Spades','Hearts','Dimonds','Clubs');@Cards=('A','2','3','4','5','6','7','8','9','10','J','Q','K');@Cards=('A','2','3','4','5','6','7','8','9','10','J','Q','K');

$CardNumber=int(rand()*52);$CardNumber=int(rand()*52);

$CurrentNum=0;$CurrentNum=0;foreach $Suite (@Suites)foreach $Suite (@Suites){{ foreach $Card (@Cards)foreach $Card (@Cards) {{ $CurrentNum++;$CurrentNum++; if($CurrentNum==$CardNumber)if($CurrentNum==$CardNumber) { $PickedCard="$Card of $Suite"; }{ $PickedCard="$Card of $Suite"; } }}}}

$GuessNumber=1;$GuessNumber=1;$Done=0;$Done=0;dodo{{ print("(Guess $GuessNumber) Pick a card type [A of Spades] for example\n");print("(Guess $GuessNumber) Pick a card type [A of Spades] for example\n"); $Guess=<STDIN>;$Guess=<STDIN>; chomp($Guess);chomp($Guess); if($Guess eq $PickedCard)if($Guess eq $PickedCard) {{ print("Success!\n");print("Success!\n"); $Done=1;$Done=1; }} elsif($Guess eq "quit")elsif($Guess eq "quit") {{ $Done=1;$Done=1; }} $GuessNumber++;$GuessNumber++;} until($Done==1);} until($Done==1);print("It was $PickedCard\n");print("It was $PickedCard\n");