CS 105 Perl: Data Types Nathan Clement 15 May 2014.

Slides:



Advertisements
Similar presentations
Arrays A list is an ordered collection of scalars. An array is a variable that holds a list. Arrays have a minimum size of 0 and a very large maximum size.
Advertisements

Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Lists Introduction to Computing Science and Programming I.
Week 2: Primitive Data Types 1.  Programming in Java  Everything goes inside a class  The main() method is the starting point for executing instructions.
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.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Intro to Robots Robots are Everywhere. Intro to Robots Various robots in use today lawnmower pet baby seal for convalescents gutter cleaner home security.
Data types and variables
Arrays. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
JavaScript, Fourth Edition
JavaScript, Third Edition
Subroutines. aka: user-defined functions, methods, procdures, sub-procedures, etc etc etc We’ll just say Subroutines. –“Functions” generally means built-in.
Arrays. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Objectives You should be able to describe: Data Types
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Ruby! Useful as a scripting language – script: A small program meant for one time use – Targeted towards small to medium size projects Use by: – Amazon,
2440: 211 Interactive Web Programming Expressions & Operators.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
1 Working with Data Types and Operators. 2 Using Variables and Constants The values stored in computer memory are called variables The values, or data,
4 1 Array and Hash Variables CGI/Perl Programming By Diane Zak.
DHTML AND JAVASCRIPT Genetic Computer School LESSON 5 INTRODUCTION JAVASCRIPT G H E F.
20-753: Fundamentals of Web Programming 1 Lecture 12: Javascript I Fundamentals of Web Programming Lecture 12: Introduction to Javascript.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Meet Perl, Part 2 Flow of Control and I/O. Perl Statements Lots of different ways to write similar statements –Can make your code look more like natural.
Perl Practical(?)‏ Extraction and Report Language.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
Introduction to Perl Yupu Liang cbio at MSKCC
Comments in PHP In PHP, we use // to make a singleline comment or /* and */ to make a large comment block. Comment is a part of your PHP code that will.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
Introduction to Java Java Translation Program Structure
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Topic 4:Subroutines CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 4, pages 56-72, Programming Perl 3rd edition pages 80-83,
CS 105 Perl: Basic I/O, Context, Strings, Lists Nathan Clement January 22, 2014.
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
More Perl Data Types Scalar: it may be a number, a character string, or a reference to another data type. -the sigil $ is used to denote a scalar(or reference)
Topic 2: Working with scalars CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 2, pages 19-38, Programming Perl 3rd edition chapter.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script.
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Chapter 14 JavaScript: Part II The Web Warrior Guide to Web Design Technologies.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
Basic Scripting & Variables Yasar Hussain Malik - NISTE.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Operators. Perl has MANY operators. –Covered in Chapter 3 of Camel –perldoc perlop Many operators have numeric and string version –remember Perl will.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
String and Lists Dr. José M. Reyes Álamo.
Ruby: An Introduction Created by Yukihiro Matsumoto in 1993 (named after his birthstone) Pure OO language (even the number 1 is an instance of a class)
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
String Conversion and Type Juggling
Data Types, Identifiers, and Expressions
Control Structures: if Conditional
Control Structures: for & while Loops
Context.
PHP.
Introduction to Primitive Data types
String and Lists Dr. José M. Reyes Álamo.
Introduction to Primitive Data types
Presentation transcript:

CS 105 Perl: Data Types Nathan Clement 15 May 2014

Agenda Paper Survey Perl’s basic data types – Scalars – Arrays – Hashes Definedness Truth Basic control flow –if statements –while loops

Carriage Returns /usr/bin/perl;^M: bad interpreter: No such file or directory UNIX/Windows environment problem – Newline Newline – Fixing it Fixing it

Types of variables Many languages such as C, C++, and Java: Primitive data types – Integers, characters, floating-point numbers, booleans Composite data types – Arrays, Structures, Classes In Perl Singular: scalars Plural – arrays – hashes

Sigils A sigil is a prefix that denotes the type of the value being specified Sigils for Perl’s fundamental data types: $ for for arrays % for hashes

Sigils You might be asking at this point, – What are all those signs for? My response would be: – Watch your mouth, buddy!

Scalars Scalars can store both numbers and strings. The following are all valid values for scalars: "" (empty string) "Just a string"

Example Scalars Initializing scalars with constants $zero = 0; $pi = ; $empty = ""; $foo = "just a string"; $atoms = 6.022e23;

Identifiers Identifiers are the names of variables. Valid identifiers in Perl Must begin with a letter or underscore Can contain letters, numbers, and underscores Are case sensitive ( Foo and foo are distinct) Like C, Java, and many other languages

Manipulating numeric scalars You can do typical arithmetic with Perl scalars. $m = ($y2 - $y1) / ($x2 - $x1); $y = $m * $x + $b; $a += $b; # same as $a = $a + $b; $a++; # $a += 1; Perl even has an exponentiation operator: ** $result = $base ** $exponent;

Manipulating string scalars You can manipulate string values, too. Concatenation (. operator): $a = "foo"; $b = "bar"; $c = $a. $b; # "foobar" $c.= $a; # "foobarfoo" For more operators, see perlop.

Sigils: Example $a is a is an array %a is a hash Remember that a sigil denotes the type of the value, not the type of the variable. For example, $a[0] is a scalar member of the

Using sigils Sigils denote the type of the value, not the type of the variable. $a is a scalar value stored in the scalar variable $a. $a[0] is a scalar value stored in the $a{“foo”} is a scalar value stored in the hash %a. The three data types have separate namespaces: and %a can all coexist Sigil rule

Arrays and Hashes: An Overview Arrays and Hashes are containers or collections store scalars Arrays ) ordered indexed by integers their index is specified inside square brackets [ ] Hashes ( % ) unordered indexed by strings (called keys ) their index is specified inside curly brackets { }

Setting and Using Array Elements $a[0] = "foo"; $a[1] = "bar"; $a[100] = 1; $a[2] = $a[0]. $a[$a[100]]; # "foobar" Although we’re using the scalar sigil ( $ ), all the data we’ve modified is You can copy

Setting and Using Hash Elements $a{"foo"} = "bar"; $a{"bar"} = "quux"; $a{"foobaz"} = $a{"foo"}. $a{$a{"foo"}}; print $a{"foobaz"}; # displays "barquux" Although we’re using the scalar sigil ( $ ), all the data we’ve modified is in %a. You can copy hashes, too. %b = %a;

Sigil Rule How to tell the difference? Remember dwimmy? Context

Definedness We can refer to Perl variables that technically don’t exist. Such variables are undefined. If we’ve never set the value of a scalar, it’s undefined. # no scalars have been defined yet $a = 10; But we can use such a variable, and Perl won’t complain (by default). Its value will be undefined, however. The undefined value is called undef.

Definedness, continued # no scalars have been defined yet $a = 10; $a = $b; But $b has never been initialized; it is undef. So $a has been set to undef.

Controlling Definedness Variables can be set to undef in two ways. setting a variable to undef (noun form) undefining a variable with undef (verb form) $a = undef; # undef as noun undef $b; # undef as verb Setting a variable to any other value causes it to be defined.

Definedness for Arrays and Hashes undef can be used to undefine arrays and hashes, too, but only in the verb form. ceases to exist undef %b; # Goodbye, = undef; # WRONG # = (undef); An empty array is not undef, nor is an empty hash.

Testing for Definedness Test whether a variable is defined with defined. $a = 10; defined($a); # returns true defined($b); # returns false

Truth Five values in Perl are false. undef "" 0 "0" () Everything else is true. These rules are defined in perlsyn at the Truth and Falsehood heading.

Manipulating Truth Perl has the following logical operators: Negation ! Logical and && Logical or || just like C, C++, Java…

The Truthiness of Truth Operators How negation ( ! ) works: !$a returns the empty string if $a is true !$a returns 1 if $a is false

More Truthiness of Truth Operators How logical and ( && ) works: $a && $b returns $a if $a is false $b otherwise How logical or ( || ) works: $a || $b returns $a if $a is true $b otherwise

A word about functions Perl comes with a lot of built-in functions. We’ve used several of them already: print defined undef To learn about the rest, see perlfunc.

if statement A simple example of an if statement: if ($a) { print "the variable is true\n"; }

if with else if ($rich) { print "I am the 1\%\n"; } elsif ($poor) { print "I’m economically disadvantaged\n"; } else { print "I’m disappearing!!\n"; } How to handle the age-old “Dangling Else” problem

A Linguistic Twist Perl allows conditionals to follow the statement they conditionalize. print "true\n" if $a; This is described in perlsyn under the heading Statement Modifiers.

A Linguistic Contortion A statement modifier can modify multiple statements, but they must be wrapped in a do block. do { print "true\n"; rejoice($a); } if $a;

More Linguistic Awesomeness Perl includes an unless keyword that can be used in the place of if, but the conditional is reversed. do { print "oh no!\n"; emergency($a); } unless $a;

while loop A simple while loop: while ($a > 0) { print $a." bottles of beer.\n"; $a--; }

until loop until is like while with the conditional reverse (just like if and unless ). until ($a <= 0) { print $a." bottles of beer.\n"; $a--; }

while at the end The loop keywords while and until can be used as statement modifiers, too. $a-- until ($a <= 0);

Statement modifiers vs. Expectations Perl has a special case for do blocks modified by while and until. Normally the conditional in the statement modifier is evaluated first. Not in this case: do { $a--; print "Mmmm, beer.\n"; } until ($a <= 0); Perl will behave according to your expectations here, but note that Perl is being dwimmy.