Introduction to perl_operators
-
Upload
vamshi-santhapuri -
Category
Technology
-
view
121 -
download
0
description
Transcript of Introduction to perl_operators
DAY 4Operators / Control Structures
Vamshi Krishna S
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
++increment -- decrement
++x pre increment x++ post increment
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
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
C-Style Perl Style What it means && and logical AND || or logical OR ! not logical NOT
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
$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
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. }
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 }
print "how old are you? "; $a = <STDIN>; chomp($a); unless ($a < 18) { print "Old enough! Cool! So go vote!\n"; $voter++;
#!/usr/bin/perl for($i = 1; $i < 5; $i++) {
# PRINT A NEW ROW EACH TIME THROUGH W/ INCREMENT
print "$i This is row $i”; }
#!/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++;
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.