Top Banner
DAY 4 Operators / Control Structures Vamshi Krishna S
14

Introduction to perl_operators

Jun 17, 2015

Download

Technology

Perl Presentation slides during 2011
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to perl_operators

DAY 4Operators / Control Structures

Vamshi Krishna S

Page 2: Introduction to perl_operators

Operator What it Does For Example Results In + Addition 3 + 4 7 - Subtraction (2 operands) 4 - 2 2 Negation (1 operand) -5 -5 * Multiplication 5 * 5 25 / Floating-point division 15 / 4 3.75 ** Exponent 4**5 1024 % Modulus (remainder) 15 % 4 3

Page 3: Introduction to perl_operators

++increment -- decrement

++x pre increment x++ post increment

Page 4: Introduction to perl_operators

Test Numeric Operator String Operator equals == eq not equals != ne less than < lt greater than > gt less than or equals <= le greater than or equals >= ge

Page 5: Introduction to perl_operators

4 < 5 # true 4 <= 4 # true 4 < 4 # false 5 < 4 + 5 # true (addition performed first) 6 < 10 > 15 # syntax error; tests cannot be

combined ‘5’ < 8 # true; ‘5’ converted to 5 ‘add’ < ‘adder’ # use lt for strings; this is an error

under -w ‘add’ lt ‘adder’ # true ‘add’ lt ‘Add’ # false; upper and lower case are

different ‘add’ eq ‘add ‘ # false, spaces count

Page 6: Introduction to perl_operators

C-Style Perl Style What it means && and logical AND || or logical OR ! not logical NOT

Page 7: Introduction to perl_operators

Operator Example Longhand equivalent

+= $x += 10 $x = $x + 10 -= $x -= 10 $x = $x - 10 *= $x *= 10 $x = $x * 10 /= $x /= 10 $x = $x / 10 %= $x %= 10 $x = $x % 10 **= $x **= 10 $x = $x**10

Page 8: Introduction to perl_operators

$x = 81; $add = $x + 9; $sub = $x - 9; $mul = $x * 10; $div = $x / 9; $exp = $x ** 5; $mod = $x % 79; print "$x plus 9 is $add"; print "$x minus 9 is $sub"; print "$x times 10 is $mul"; print "$x divided by 9 is $div"; print "$x to the 5th is $exp"; print "$x modulus 79 is $mod

Page 9: Introduction to perl_operators

A statement block is a sequence of statements, enclosed in matching curly braces. It looks like this:

{ first_statement; second_statement; third_statement; ... last_statement; # last statement semicolon is optional. }

Page 10: Introduction to perl_operators

print "how old are you? "; $a = <STDIN>; chomp($a); if ($a < 18) { print "So, you're not old enough to vote, eh?\n"; } else { print "Old enough! Cool! So go vote!\n"; $voter++; # count the voters for later }

Page 11: Introduction to perl_operators

print "how old are you? "; $a = <STDIN>; chomp($a); unless ($a < 18) { print "Old enough! Cool! So go vote!\n"; $voter++;

Page 12: Introduction to perl_operators

#!/usr/bin/perl for($i = 1; $i < 5; $i++) {

# PRINT A NEW ROW EACH TIME THROUGH W/ INCREMENT

print "$i This is row $i”; }

Page 13: Introduction to perl_operators

#!/usr/bin/perl # CREATE AN ARRAY @names = qw(Steve Bill Connor

Bradley); # SET A COUNT VARIABLE $count = 1; # BEGIN THE LOOP foreach $names(@names) { Print “$count $names; $count++;

Page 14: Introduction to perl_operators

http://perldoc.perl.org/perlreref.html#ESCAPE-SEQUENCES

Eg: \a Alarm (beep) \n Newline \r Carriage return \t Tab

(\l, \u, \L, \U,and \E)… makes the letters or strings upper or lower case until the end of the string or \E occurs.