Introduction to perl_operators
-
Upload
vamshi-santhapuri -
Category
Technology
-
view
121 -
download
0
description
Transcript of Introduction to perl_operators
![Page 1: Introduction to perl_operators](https://reader036.fdocuments.net/reader036/viewer/2022062707/55814e86d8b42ab9548b4ace/html5/thumbnails/1.jpg)
DAY 4Operators / Control Structures
Vamshi Krishna S
![Page 2: Introduction to perl_operators](https://reader036.fdocuments.net/reader036/viewer/2022062707/55814e86d8b42ab9548b4ace/html5/thumbnails/2.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062707/55814e86d8b42ab9548b4ace/html5/thumbnails/3.jpg)
++increment -- decrement
++x pre increment x++ post increment
![Page 4: Introduction to perl_operators](https://reader036.fdocuments.net/reader036/viewer/2022062707/55814e86d8b42ab9548b4ace/html5/thumbnails/4.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062707/55814e86d8b42ab9548b4ace/html5/thumbnails/5.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062707/55814e86d8b42ab9548b4ace/html5/thumbnails/6.jpg)
C-Style Perl Style What it means && and logical AND || or logical OR ! not logical NOT
![Page 7: Introduction to perl_operators](https://reader036.fdocuments.net/reader036/viewer/2022062707/55814e86d8b42ab9548b4ace/html5/thumbnails/7.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062707/55814e86d8b42ab9548b4ace/html5/thumbnails/8.jpg)
$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](https://reader036.fdocuments.net/reader036/viewer/2022062707/55814e86d8b42ab9548b4ace/html5/thumbnails/9.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062707/55814e86d8b42ab9548b4ace/html5/thumbnails/10.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062707/55814e86d8b42ab9548b4ace/html5/thumbnails/11.jpg)
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](https://reader036.fdocuments.net/reader036/viewer/2022062707/55814e86d8b42ab9548b4ace/html5/thumbnails/12.jpg)
#!/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](https://reader036.fdocuments.net/reader036/viewer/2022062707/55814e86d8b42ab9548b4ace/html5/thumbnails/13.jpg)
#!/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](https://reader036.fdocuments.net/reader036/viewer/2022062707/55814e86d8b42ab9548b4ace/html5/thumbnails/14.jpg)
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.