Operators. Perl has MANY operators. –Covered in Chapter 3 of Camel –perldoc perlop Many operators have numeric and string version –remember Perl will.

Slides:



Advertisements
Similar presentations
Operators and Expressions Operators are symbols that produce a result based on some rules. Examples: +, -, =, > and
Advertisements

Types and Arithmetic Operators
Control Structures Control structures are used to manage the order in which statements in computer programs will be executed Three different approaches.
CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.
Scalar Variables Start the file with: #! /usr/bin/perl –w No spaces or newlines before the the #! “#!” is sometimes called a “shebang”. It is a signal.
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Bash, part 2 Prof. Chris GauthierDickey COMP Unix Tools.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
CS 3850 Lecture 5 Operators. 5.1 Binary Arithmetic Operators Binary arithmetic operators operate on two operands. Register and net (wire) operands are.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
Operators. Perl has MANY operators. –Covered in Chapter 3 of Prog.Perl Most operators have numeric and string version –remember Perl will convert variable.
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
JavaScript, Third Edition
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations System-oriented Programming, B. Hirsbrunner,
Chapter 4: Basic C Operators
Expressions & Assignments One of the original intentions of computer programs was the evaluation of mathematical expressions –languages would inherit much.
Chapter 4: Operators Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills /1436.
OperatorstMyn1 Operators The sequence in which different operators in an expression are executed is determined by the precedence of the operators. Operators.
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Numeric Types, Expressions, and Output ROBERT REAVES.
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
Control Structures. Important Semantic Difference In all of these loops we are going to discuss, the braces are ALWAYS REQUIRED. Even if your loop/block.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
Perl Practical(?)‏ Extraction and Report Language.
CSI 3120, Expressions and assignment, page 1 Expressions and assignment Points for discussion Arithmetic expressions Overloading Logical expressions Assignment.
C Programming Lecture 6 : Operators Lecture notes : courtesy of Ohio Supercomputing Center, and Prof. Woo and Prof. Chang.
C++ Basics Tutorial 6 Operators. What are going to see today? Assignment operator(=) Arithmetic operators(+,-,*,/,%) Compound assignment(+=,-=,*=……..)
Perl Language Yize Chen CS354. History Perl was designed by Larry Wall in 1987 as a text processing language Perl has revised several times and becomes.
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
15-Nov-15 All the Operators. operators.ppt 2 Precedence An operator with higher precedence is done earlier (precedes) one with lower precedence A higher.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
Topic 2: Working with scalars CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 2, pages 19-38, Programming Perl 3rd edition chapter.
Introduction to Perl NICOLE VECERE. Background General Purpose Language ◦ Procedural, Functional, and Object-oriented Developed for text manipulation.
Expressions and Operators in C. Expressions and Operators Examples: 3 + 5; x; x=0; x=x+1; printf("%d",x); Two types: – Function calls – The expressions.
Operators & Expressions
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
 Array ◦ Single & Multi-dimensional  Java Operators ◦ Assignment ◦ Arithmetic ◦ Relational ◦ Logical ◦ Bitwise & other.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Built-In Functions. Notations For each function, I will give its name and prototype. –prototype = number and type of arguments ARRAY means an actual named.
Rational Expressions relational operators logical operators order of precedence.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
Operators & Expressions
CSE 220 – C Programming Expressions.
Chap. 2. Types, Operators, and Expressions
University of Central Florida COP 3330 Object Oriented Programming
2.5 Another Java Application: Adding Integers
University of Central Florida COP 3330 Object Oriented Programming
Relational Operations
Arithmetic operations, decisions and looping
All the Operators 22-Nov-18.
Context.
All the Operators 4-Dec-18.
Chapter-3 Operators.
All the Operators 6-Apr-19.
All the Operators 13-Apr-19.
The Selection Structure
OPERATORS AND EXPRESSIONS IN C++
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
OPERATORS in C Programming
DATA TYPES AND OPERATIONS
ICS 101 Lab 3 Hossain Arif ICS Dept
OPERATORS in C Programming
Presentation transcript:

Operators

Perl has MANY operators. –Covered in Chapter 3 of Camel –perldoc perlop Many operators have numeric and string version –remember Perl will convert variable type for you. We will go through them in decreasing precedence.

Increment/Decrement ++ and -- –Prefix and Postfix work as they do in C/C++ –my $y = 5; my $x = $y++; $y  6, $x  5 –my $y = 5; my $x = ++$y; $y  6; $x  6

Incrementation Magic ++ is "magical". (-- is not) –if value is purely numeric, works as expected –if string value, magic happens –my $v = '99'; $v++;  '100' –my $w = 'a9'; $w++;  'b0' –my $x = 'Az'; $x++;  'Ba' –my $y = 'zz'; $y++;  'aaa' –my $z = 'zZ9'; $z++;  'aaA0'

Even better… In addition to that magic, ++ will also automatically convert undef to numeric context, and then increment it. #!/usr/bin/env perl use strict; use warnings; my $a; $a++; print "$a\n"; prints 1 with no warnings or errors No need to initialized numeric variables to 0.

Exponentiation **  Exponentiation. works on floating points or integers 2**3  pow(2, 3)  "2 to the power of 3"  8 NOTE: higher precedence than negation -2**4  -(2**4)  -16

Unary Operators logical negation: ! –my $x = 5; my $y = 'alpha'; my $z; if(!$x) : false if (!$y) : false if (!$z) : true if : true arithmetic negation: - –$x = -5; $y = -$x; $y = 5 string negation: - –my $foo = '-abc'; my $bar = -$foo; –$bar = '+abc' bitwise negation: ~ –my $x = 5; 0b –my $y = ~$x; $y = 0b –4,294,967,290

Multiplicative / -- Division. Done in floating point. % -- Modulus. Truncates operands * -- Numeric multiplication –123 * 3  369 x -- String multiplication (aka repetition). –123 x 3  ' ' (scalar context) –(123) x 3  (123, 123, 123) (1, 2, 3) x 3  (1, 2, 3, 1, 2, 3, 1, 2, 3) (list context)

Additive + -- addition - -- subtraction. -- string concatenation –$var1 = 'hello'; $var2 = 'world'; –$var3 = $var1. ' '. $var2; $var3 contains "hello world" –usually easier to just use interpolation: –$var3 = "$var1 $var2"; $var3 contains "hello world"

Shift operators > Shift bits in left argument number of places in right argument 1 << 4  16 – b << 4  b  16d 32 >> 4  2 – b >> 4  b  2d

Relational Operators NumericStringMeaning >gt Greater Than >=ge Greater Than or Equal <lt Less Than <=le Less Than or Equal String1 is "less than" String2 if the first differing character appears first on the ASCII chart. –"abc" is less than "add" –shorter substring comes before whole string "abc" is less than "abcd"

Equality Operators NumericStringMeaning ==eq Equal to !=ne not equal to cmp comparison About the comparison operators: -1 if left < right 0 if left == right 1 if left > right

The danger of mixing contexts my $s1 = 'Foo Bar'; my $s2 = 'Hello World'; if ($s1 == $s2){print "Yes\n"; } –The == operator expects two numbers. Converts both strings to 0. 0 == 0 use warnings; will warn you that these variables are non-numeric chomp($x = ); #gives 42 chomp($y = ); #gives if ($x eq $y) {print "Yes\n"; } –'42' is not the same string as '42.00'

Bitwise Operators & -- AND. | -- OR ^ -- XOR –& has higher precedence if either value numeric: –convert to integer, –bitwise comparison on integers if both values strings: –bitwise comparison on corresponding bits from the two strings

Logical Operators && - AND || - OR –&& has higher precedence operate in short-circuit evaluation –ie, evaluate only what's needed –creates this common Perl line: open (my $fh, '<', 'file.txt') || die "Can't open file.txt: $!"; return last value evaluated, not simply "true" or "false" –$x = 0 || 5 || 3; $x gets value 5. –$y = 5 && '' && 3; $y gets value '' –$a = 0 || "" || undef; $a gets value undef –$b = 5 && 3 && 1 $b gets value 1

Logical defined-or (5.10 exclusive) Tests the defined'ness of the lefthand operator, rather than the truth. If left is defined, returns left. Else, return right. my $x = $a // $b; my $x; if (defined $a) { $x = $a; } else { $x = $b }; Useful for setting defaults on values that may be 0 or '' : my $value = $args[0] // 'default'; contrast with: my $value = $args[0] || 'default';

Range operator Generates a list of numbers or strings = (1..10); = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10); = ('e'.. 'k'); = ('e','f','g','h','i','j','k'); = ('ax'.. 'bc'); = ('ax','ay','az','ba','bb','bc'); Always increments by 1 –same effect as ++ operator if left is greater than right, returns empty list if one of the operands is numeric and the other is not, non-numeric will be used in numeric context, producing a warning.

Conditional Operator ?: -- Trinary operator in C. like an if-else statement, but it's an expression –my $a = EXPR ? $b : $c; –if EXPR is true, $a = $b. –if EXPR is false, $a = $c my $status = $grade >= 65 ? 'pass' : 'fail';

Assignment operators =, **=, *=, /=, %=, x=, +=, -=,.=, &=, |=, ^=, >=, &&=, ||=, //= In all cases, all assignments of form TARGET OP= EXPR evaluate as: TARGET = TARGET OP EXPR

Comma Operator Scalar context: –evaluate each list element, left to right. Throw away all but last value. –$a = (fctn(), fctn2(), fctn3()); Call fctn(), call fctn2(), call fctn3() and assign $a to fctn3()'s return value –All three functions called in scalar context –There is no such thing as a list in scalar context! List context: –list element separator, as in array assignment = (fctn(), fctn2(), gets return values of all three functions –all functions called in list context –not necessarily three values! functions can also return lists!

Logical and, or, not, xor Functionally equivalent to &&, ||, ! BUT, a lower precedence. $xyz = $x || $y || $z; $xyz = $x or $y or $z; What's the difference? (There is no lower-precedence version of //, and there is no higher-precedence version of xor)

Incomplete list ALL operators found in Chapter 3 of Camel. –And in perldoc perlop some skipped over, we'll talk about them later. (arrow, file test, binding)