Scalar Data Types and Basic I/O

Slides:



Advertisements
Similar presentations
Chapter 25 Perl and CGI (Common Gateway Interface)
Advertisements

Chapter Three Arithmetic Expressions and Assignment Statements
Computer Programming w/ Eng. Applications
Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – The Master Manipulator perl Perl is.
Introduction to Perl. How to run perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Your program/script.
Data types and variables
1 Key Concepts:  Data types in C.  What is a variable?  Variable Declaration  Variable Initialization  Printf()  Scanf()  Working with numbers in.
1ex.1 Perl Programming for Biology Exercise 1 The Bioinformatics Unit G.S. Wise Faculty of Life Science Tel Aviv University, Israel March 2009 Eyal Privman.
Chapter 2 Data Types, Declarations, and Displays
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Basic Elements of C++ Chapter 2.
Objectives You should be able to describe: Data Types
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
Elements of a C++ program 1. Review Algorithms describe how to solve a problem Structured English (pseudo-code) Programs form that can be translated into.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
Input, Output, and Processing
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
C++ Programming: Basic Elements of C++.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Control Structures (A) Topics to cover here: Introduction to Control Structures in the algorithmic language Sequencing.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Computer Programming for Biologists Class 3 Nov 13 th, 2014 Karsten Hokamp
Introduction to Perl “Practical Extraction and Report Language” “Pathologically Eclectic Rubbish Lister”
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Topic 2: Working with scalars CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 2, pages 19-38, Programming Perl 3rd edition chapter.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Programming Fundamentals
Programming Fundamentals with C++1 Chapter 3 COMPLETING THE BASICS.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
COMP 110: Spring Announcements Lab 1 due Wednesday at Noon Assignment 1 available on website Online drop date is today.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
CS201 Introduction to Sabancı University 1 Chapter 2 Writing and Understanding C++ l Writing programs in any language requires understanding.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
1 Arrays and Pointers The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
Expressions Version Topics Arithmetic expressions Conversions Operator precedence String class 2.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
M105 - Week 2 Chapter 3 Numerical Data 1 Prepared by: M105 Team - AOU - SAB.
Chapter 16 Introduction to Perl Perl (Practical Extraction and Report Language) Developed by Larry Wall as a Unix scripting language. Popular server-side.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter 1.2 Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
Topics Designing a Program Input, Processing, and Output
Chapter 1.2 Introduction to C++ Programming
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
BASIC ELEMENTS OF A COMPUTER PROGRAM
Computing Fundamentals
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Basic Elements of C++.
Chapter 2 Assignment and Interactive Input
Revision Lecture
Basic Elements of C++ Chapter 2.
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Introduction to C++ Programming
Programming Funamental slides
A First Book of ANSI C Fourth Edition
Programming Funamental slides
Introduction to C++ Programming
CS150 Introduction to Computer Science 1
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Presentation transcript:

Scalar Data Types and Basic I/O

Variables in Perl You DO NOT have to declare variables in Perl. Unless you force it to force you to declare variables. Three basic types of variables: Scalar Array Hashes

Scalars Scalar variables store “single values”. This “single value” can be any of the following types: int, float, double, char, string, or references. In Perl these various types fall into one of three categories: numbers, strings, and references. You don’t have to declare a variable’s type.

Scalars: Declaration All scalar variables begin with a $. $ is NOT part of the variable! Next character is either a letter or ‘_’. Remaining characters are a mix of letters, numbers, and ‘_’. Correct: $x, $myvar123, $avg_height Wrong: $2values, $avg-height, $good$day.

Scalars: Numbers Unlike C/C++, all numeric data in Perl are stored as double precision floating points. Ex: 37 3.7 .37 37. 3E7 3e7 3E-7 Hex: 0x2aff, 0xAA3, 0XFFF Octal: 077 0276 Underscore: 3_212_567 → 3,212,567. Again, you do not need to specify the type.

Numeric Operators Operator Associativity ++ -- none unary - right ** * / % left binary + - ++ and – have the highest precedence.

Examples 4 % 2 → 0 5 / 2 → 2.5 (5 and 2 are coerced from integers to reals). $total++ * 3 $a ** 2 $b / $ c / 2 Important. The order of evaluation of operands of operators is unspecified. This is left for the compiler to decide. Ex: $x++ * $x--

Scalars: Strings Unlike C/C++, strings in Perl are not terminated by ‘\0’. They are not represented as an array of characters. Individual characters cannot be accessed with [ ]. There are two types of strings in Perl Single quoted strings (‘). Double quoted strings (“).

Single Quoted Strings Strings delimited by (‘). They are not interpolated and cannot contain escape sequence characters. Examples: ‘hello’ ‘Don\’t do it!’ ‘can\’t, won\’t, wouldn\’t ever!’ ‘apples are good\n’ ??

Single Quoted Strings Another to say the same thing: q^hello^ q^apples are good\t^ q^Don’t do it!^ q^can’t, won’t, wouldn’t ever!^ Can also use ( ), { }, [ ], < > for better readability: Q(hello) Q{can’t, won’t, wouldn’t ever!}

Double Quoted Strings. Differs from single quoted strings in two ways: Can include escape sequence characters – e.g. \n, \t, \r, etc. Variables in the string are interpolated. Examples: “The man said, \”Quantity \t Price \t Total\” \n\n” “Apples are good for $name.” → “Apples are good for bob.” “Apples are good for \$name.” → “Apples are good for $name.” “Today is ${day}day.” → “Today is Monday.” Can also use qq Ex: qq*”No way!”, said the girl.*

When q Meets qq What happens when you put (‘) and (“) together? If “ is embedded within ‘ ‘The boy said, “Today is $day” ’ If ‘ is embedded within “ “The boy said, ‘Today is $day’ “

String Operators String Catenation (.) Append two strings together. “Happy” . “ Birthday” → “Happy Birthday.” $str . “ Holidays” → “Happy Holidays.” The operands are not effected by (.) Repetition operator (x) “Beat OU! ” x 3 → “Beat OU! Beat OU! Beat OU! ” What about? “Happy ” . “Birthday! ” x 2 → “Happy Birthday! Birthday! ”

String Functions Perl provides several useful string manipulation functions. chop and chomp length, lc, uc ord, hex, oct index, rindex substr, join

Chop and Chomp Chop removes the last character in a string. chop(“apples”) → “apple” If $a, $b, and $c are “a”, “an”, and “ant”, then chop($a, $b, $c) → “” “a” “an” Chomp removes the ending input record separator (e.g. newline) in a string. If string does not end with an input record separator, then chomp does nothing to the string and returns 0.

length, lc, and uc length returns the number of chars. Ex: length(“apples”) → 6 lc converts a string to all lower case. Ex: lc(“ApPlEs”) → “apples” uc converts a string to all upper case. Ex: uc(“apples”) → “APPLES”

index and rindex index searches for the starting position of a substring. rindex same as index except search is done from right to left. Examples: index(“apples”, “pp”) returns 1 rindex(“apples”, “pp”) returns 1 index(“apples”, “p”) returns 1 rindex(“apples”, “p”) returns 2 index(“apples”, “q”) returns -1

substr substr extracts a substring The way to call it is: Examples: substr(string, position, length) Examples: substr(“fruit juice”, 0, 3) returns “fru” substr(“fruit juice”, 3, 5) returns “it ju” substr(“fruit juice”, -3, 3) returns “ice”

join Like (.) but appends several strings separated by a deliminator. The way to call it is: join Expression, List Example: $month = “09”, $day = “01”, $year = “05” join ‘/’, $month, $day, $year → “09/01/05”. join ‘/’, $month, $day, 2005 → ??

Mixed Modes What happens when strings and numbers interact? The output depends on the context. Examples: $str = “32abc” 7 + $str 7 . $str What if $str = “abc32” → 39 → “732abc” → 7

Assignments Simple assignment operators (=) $x = 2; $average = $sum / $total; $x = $y = $b = 2; $result = 17 * ($sum = $x + $y); chomp($str = $str1. $str1); Compound assignment operators (<op>=) $sum += $new_value; $str .= “ing”; $result **= 4;

Basic I/O <STDIN> reads input from a keyboard $new_input = <STDIN> print writes output to screen print “Hello world!\n”; print (“Was summer vacation fun?\n”); print (“The sum is: $sum”, “\tThe average is: $average\”);

A Simple Example # circle # Input: The radius of a circle. # Output: The area and circumference of the circle. $pi = 3.14; print “Please enter the radius of the circle: ”; $radius = <STDIN>; $area = $pi * $radius * $radius; $circumference = $pi * 2 * $radius; print “A circle of radius $radius has an area of $area \n”, “ and a circumference of $circumference \n”;