170 likes | 376 Views
Perl. Day 3. 1 + 1 = 2. We’ve previously seen you can do math on variables $Num=1 $Num2=$Num+7; print(“$Num2<br>”); You can even do math on the same variable: $Num1=$Num1+7; A commonly used short cut for adding 1 to a number is: $Num1++; Likewise you can subtract one: $Num1--;
E N D
Perl Day 3
1 + 1 = 2 • We’ve previously seen you can do math on variables $Num=1 $Num2=$Num+7; print(“$Num2\n”); • You can even do math on the same variable: $Num1=$Num1+7; • A commonly used short cut for adding 1 to a number is: $Num1++; • Likewise you can subtract one: $Num1--; • Another shortcut allows you to add a number to a variable (This is the old way to do that: $Num1=$Num1+7:) $Num1+=7; • That works for more than addition, you can multiply, subtract or divide. $Num1*=2; $Num1/=5;
Nap time! • Computers go very fast, a 2.8GHz processor can do 2.8 billion instructions per second. Many of our computers in the datacenter have 8 cores at 2.8Ghz, meaning they can do well over 20 billion instructions per second. • Sometimes in the real world we’d like to go a little slower, so we can ask the computer to take a nap while it waits for us: sleep(10);
Asking the user questions • What if you want your script to ask the user a question • There is a magic “file” which corresponds to the keyboard, called STDIN (standard input). • It is already open, you don’t have to open, or close it like a file, you can simply use it. • If you assign <STDIN> to a variable the program will stop and wait for the user to enter 1 line. print(“Enter a number\n”); $Num=<STDIN>; print(“Thanks you gave me $Num\n”);
Gobble gobble…chomp chomp • Most lines you read in from a file, or keyboard, ended with a \n • Usually in a script you don’t want the \n on the end of the string already, because you are going to print(“$string\n”); • chomp safely removes any \n’s from the end: $Answer=<STDIN>; chomp($Answer);
Picking a Random Number • The perl function rand() picks a number between 0 and 1. • If you want a random number between 1 and 100, you’d do: $RandNum=int(rand()*100); • This says pick a number between 0 and 1. Multiply that result by 100 (which gives you something like 54.242482. We only care about the 54, which “int” returns only the integer part
Push it real good… • On day 1 we mentioned arrays @Days=(‘mon’,’tue’,’wed’,’thu’,’fri’); • What if we now wanted to add the weekend? The array is already defined. push(@Days,'sat'); • Now, lets assume Sun should be first: Unshift(@Days,’sun’);
Pop goes the weasel • Well if we have push, we must have pop: $LastDay=pop(@Days); • Note pop pulls from the end of the array • Shift pulls from the front $FirstDay=shift(@Days);
Ahh…I’m getting loopy • Sometimes you want to do something more than once. • Sometimes you want to do something more than once • This is called a loop. There are 4 types of loops, usually you can do the same job with all 4, it’s just some are easier for one situation or another. • The first 3 are almost identical
While • A while loop continue to do whatever is between the {}’s until the Test part is false: • while(test) { … } • This will print the numbers 10, 9, 8…1 $i=10; while($i>0) { print("$i\n"); $i--; }
Do • A do loop is almost identical to a while loop, it’s just you test at the end instead of the start: $i=10; do { print("$i\n"); $i--; } until ($i<0)
For • A for loop is most commonly used when you want to just do something a set number of times (like the last example): • As you will see it’s much shorter • The for part has 3 sections separated by ; • The first part is the initialization, $i=10 • The second part is the test it runs each time through the loop, it keeps going so long as it’s true $i>0 • The 3rd part is what you’d like to do to $i each time through: $i-- for($i=10;$i>0;$i--) { print("$i\n"); }
foreach • Foreach is used when you have an array of things you want to do something to each: @Colors=(‘red’,’green’,’blue’,’yellow’); foreach $Color (@Colors) { print(“Do you like $Color [y/n]?\n”); $Answer=<STDIN>; chomp($Answer) if($Answer eq ‘y’) { print(“OK, you like $Color\n”); } }
Nested Loops • Sometimes you have 2 different things you need to go over. • Think of a deck of cards. • There are 4 suites (spades, hearts, dimonds, clubs) • There are 13 cards in each suite (A,2,3…9,10,J,Q,K) • Lets play a game of guess the card.
Picking the winning card. • We’ll need to number all the cards, and then pick a random number between 1 and 52 to pick a card. • Remember rand()…it’ll help you pick a number • To number the cards, you’ll need a loop inside a loop. • The outer loop will go across all the suites • The inner loop will go across all the cards • The 2nd loop must be inside the first, because there is an A in all 4 suites. $CardNumber=int(rand()*52); $CurrentNum=0; foreach $Suite (@Suites) { foreach $Card (@Cards) { $CurrentNum++; if($CurrentNum==$CardNumber) { $PickedCard="$Card of $Suite"; } } }
Now let the user guess • Lets use a do loop until they succeed or give up • We’ll start off assuming they aren’t done ($done=0), then if they get it right, or quit we’ll set done to 1, so our “until” will be $done=1; • Inside this loop we’ll prompt them to enter a guess • Read their answer, chomp it • Check if it’s right • Tell them they are right • Set $done=1 • Check if they gave up • Set $done=1 • Finally tell them what the random card was, just so you know it really works.
#!/usr/bin/perl @Suites=('Spades','Hearts','Dimonds','Clubs'); @Cards=('A','2','3','4','5','6','7','8','9','10','J','Q','K'); $CardNumber=int(rand()*52); $CurrentNum=0; foreach $Suite (@Suites) { foreach $Card (@Cards) { $CurrentNum++; if($CurrentNum==$CardNumber) { $PickedCard="$Card of $Suite"; } } } $GuessNumber=1; $Done=0; do { print("(Guess $GuessNumber) Pick a card type [A of Spades] for example\n"); $Guess=<STDIN>; chomp($Guess); if($Guess eq $PickedCard) { print("Success!\n"); $Done=1; } elsif($Guess eq "quit") { $Done=1; } $GuessNumber++; } until($Done==1); print("It was $PickedCard\n");