1 / 17

Perl

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--;

ginger
Download Presentation

Perl

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Perl Day 3

  2. 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;

  3. 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);

  4. 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”);

  5. 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);

  6. 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

  7. 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’);

  8. 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);

  9. 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

  10. 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--; }

  11. 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)

  12. 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"); }

  13. 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”); } }

  14. 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.

  15. 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"; } } }

  16. 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.

  17. #!/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");

More Related