• Like
  • Save
Python & Perl: Lecture 22
Upcoming SlideShare
Loading in...5
×

Python & Perl: Lecture 22

  • 1,094 views
Uploaded on

 

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
1,094
On Slideshare
0
From Embeds
0
Number of Embeds
5

Actions

Shares
Downloads
0
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Python & Perl Lecture 22 Vladimir Kulyukin Department of Computer Science Utah State Universitywww.youtube.com/vkedco www.vkedco.blogspot.com
  • 2. Outline ● Operators ● Scalar Contexts ● Control structureswww.youtube.com/vkedco www.vkedco.blogspot.com
  • 3. Notes on Final Project Proposals ● You are expected to put two hours/week into the project plus one hour for quiz-like Perl assignments ● Some project proposals were two ambitious: – OpenCV to recognize objects for robotic arms – Diet management system for Windows, Linux, and Android ● Some project proposals were too simple: – Tic-Tac-Toe with min-maxwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 4. Operatorswww.youtube.com/vkedco www.vkedco.blogspot.com
  • 5. String Operators q & qq ● q/some text/ treats some text as a single quoted string ● Delimeters after q do not have to be /, they can be { }, [ ], < >, or | | print q/C:Perlbinperl.exe/, "n"; print q[/usr/bin/perl.exe], "n"; print q{/usr/bin/perl.exe}, "n";www.youtube.com/vkedco www.vkedco.blogspot.com
  • 6. String Operators q & qq ● qq/some text/ treats some text as a double quoted string ● Delimeters after qq do not have to be /, they can be { }, [ ], < >, or | | print qq{He said, "Hello, world!"}, "n"; print qq[/usr/bin/perl.exe], "n"; print qq|"Hi," said Jack. "Have you read /. today?"n|;www.youtube.com/vkedco www.vkedco.blogspot.com
  • 7. Example q_and_qq.plwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 8. Here-Documents ● A here-document allows you to write large amounts of text in your program and treat them as strings ● A here-document starts with << immediately followed by a start label, then some text, and a terminating label that must be the same as the start label ● The terminating label must be on a separate line by itself with no semicolonwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 9. Example print <<EOT; This is a line of text. This is another line of text ... Good bye. EOTwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 10. Example here_doc_example.plwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 11. Arithmetic Operators Addition $x + $y Subtraction $x - $y Multiplication $x * $y Division $x / $y Modulus $x % $y Exponentiation $x ** $ywww.youtube.com/vkedco www.vkedco.blogspot.com
  • 12. Operator Precedence ● Operators in expressions with parentheses are evaluated first ● Exponentiation operators are applied; if there are multiple such operators, they are applied left to right ● Multiplication, division operators are applied; if there are multiple such operators, they are applied left to right ● Addition and subtraction operators applied; if there are multiple such operators, they are applied left to rightwww.youtube.com/vkedco
  • 13. Example Write a Perl program that evaluates the polynomial y = a*x^2 + b*x + c at the user-supplied values of a, x, b, c. Solution in operator_precedence.plwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 14. Assignment Operators ● Perl provides the C-style assignment operators for abbreviating arithmetic operations in assignment expressions ● Any statement of the form variable = variable operator expression; where operator is one of the binary operators (+, -, *, /) can be written in the form variable operator= expression;www.youtube.com/vkedco www.vkedco.blogspot.com
  • 15. Assignment Operators $c = $c + 5; $c += 5; $c = $c – 5; $c -= 5; $c = $c * 5; $c *= 5; $c = $c / 5; $c /= 5; $c = $c % 5; $c %= 5; $c = $c ** 5; $c **= 5;www.youtube.com/vkedco www.vkedco.blogspot.com
  • 16. Increment and Decrement Operators ++$c; increment $c by 1 and use the new value of $c in the expression where $c resides $c++; use the current value of $c in the expression in which $c resides, then increment $c by 1 --$c; Same as ++$c except $c is decremented by 1 $c--; same as $c++ except $c is decremented by 1www.youtube.com/vkedco www.vkedco.blogspot.com
  • 17. Example incr_decr_operators.plwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 18. Equality and Relational Operators > $x > $y < $x < $y >= $x >= $y <= $x <= $y == $x == $y != $x != $ywww.youtube.com/vkedco www.vkedco.blogspot.com
  • 19. Example eq_and_rel_operators.plwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 20. String Operators ● Perl provides a collection of comparison operators on string scalars ● String operators are: eq (“equals”), ne (“not equals”), lt (“less than”), gt (“greater than”), le (“less than or equal”), ge (“greater than or equal”) ● Strings are compared alphabetically, with the letters later in the alphabet having “greater” value ● “rabbit” gt “dragon” is true, because the ASCII value of r (114) > the ASCII value of d (100)www.youtube.com/vkedco
  • 21. Example string_comp.plwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 22. String Concatenation and Repetition ● The dot (.) is the string concatenation operator print “a” . “b” . “c” . “n”; ● A string s followed by the string repetition operator (x) followed by an integer n concatenates n copies of s together print “Yeah” . “!” x 3 . “n”;www.youtube.com/vkedco www.vkedco.blogspot.com
  • 23. Example string_concat.plwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 24. Scalar Contextswww.youtube.com/vkedco www.vkedco.blogspot.com
  • 25. Numeric and String Contexts ● Scalar variables can refer to strings and numbers, depending on where they occur in the program ● Perl converts the value to a string or a number depending on the context in which the scalar variable is used ● Uninitialized or undefined variables have the special value undef which evaluates differently in different contexts: in a numeric context, undef evaluates to 0; in a string context, it evaluates to “”www.youtube.com/vkedco www.vkedco.blogspot.com
  • 26. Example scalar_context.plwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 27. Control Structureswww.youtube.com/vkedco www.vkedco.blogspot.com
  • 28. if/else & if/elseif/else Selection Let us implement this pseudocode: If salespersons sales >= 100 Print “$1500 bonus!” Else If salespersons sales >= 50 Print “$200 bonus!” Else Print “You did not earn your bonus.”www.youtube.com/vkedco www.vkedco.blogspot.com
  • 29. Example if_unless.pl if_else_if.plwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 30. do/while and do/until Loops ● In addition to the standard while and until loops, Perl has do/while and do/until loops ● while and until loops check their condition first and then execute the body of the loop ● do/while and do/until check their condition after they execute the body of the loopwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 31. do/while and do/until Loops do { statements; } while ( condition ); do { statements; } until ( condition );www.youtube.com/vkedco www.vkedco.blogspot.com
  • 32. Reading & References ● http://perldoc.perl.org/ ● James Lee. Beginning Perl, 2nd Edition, APRESS ● Dietel, Dietel, Nieto, McPhie. Perl How to Program, Prentice Hallwww.youtube.com/vkedco www.vkedco.blogspot.com