Perl Arrays and Lists Learning Objectives: 1. To understand the format and the declaration of Arrays & Lists in Perl 2. To distinguish the difference between.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

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.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Dynamic Arrays Lecture 4. Arrays In many languages the size of the array is fixed however in perl an array is considered to be dynamic: its size can be.
Perl Arrays and Lists Software Tools. Slide 2 Lists l A list is an ordered collection of scalar data. l A list begins and ends with parentheses, with.
Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – The Master Manipulator perl Perl is.
Perl I/O Learning Objectives: 1. To understand how to perform input from standard Input & how to process the input 2. To understand how to perform input.
Perl Hashes Software Tools. Slide 2 “bill” “cheap” What is a Hash? l A hash (or associative array) is like an array, where the index can be any scalar.
Scripting Languages Chapter 6 I/O Basics. Input from STDIN We’ve been doing so with $line = chomp($line); Same as chomp($line= ); line input op gives.
17-Jun-15 Assorted Ruby Details. The command line irb starts an interactive Ruby interpreter ruby starts Ruby, with input from the command line End with.
Perl Functions Learning Objectives: 1. To learn how to create functions in a Perl’s program & how to call them 2. To learn how to pass [structured] arguments.
Perl Hashes Learning Objectives: 1. To understand the concept of Hash as a data structure 2. To learn the operations of Hash which are available in Perl.
Introduction to Perl Learning Objectives: 1. To introduce the features provided by Perl 2. To learn the basic Syntax & simple Input/Output control in Perl.
Perl I/O Software Tools. Lecture 15 / Slide 2 Input from STDIN Reading from STDIN is easy, and we have done it many times. $a = ; In a scalar context,
Perl Hashes Learning Objectives: 1. To understand the concept of Hash as a data structure 2. To learn the operations of Hash which are available in Perl.
Introduction to Perl Software Tools. Slide 2 Introduction to Perl l Perl is a scripting language that makes manipulation of text, files, and processes.
Perl File I/O and Arrays. File I/O Perl allows to open a file to read, write, or append As well as pipe input or output to another program. —We get to.
JavaScript, Third Edition
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
2ex.1 Lists and Arrays. 2ex.2 Comments on exercises Always run your script with “ perl -w ” and take care of all warnings  submitted scripts should not.
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
Expressions, Data Conversion, and Input
Lists in Python.
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
CPTG286K Programming - Perl Chapter 3: Arrays and List Data.
Chapter 8 Arrays and Strings
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
CS 330 Programming Languages 10 / 07 / 2008 Instructor: Michael Eckmann.
Books. Perl Perl (Practical Extraction and Report Language) by Larry Wall Perl 1.0 was released to usenet's alt.comp.sources in 1987 Perl 5 was released.
Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition.
7 1 User-Defined Functions CGI/Perl Programming By Diane Zak.
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.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
Computer Programming for Biologists Class 3 Nov 13 th, 2014 Karsten Hokamp
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
5 1 Data Files CGI/Perl Programming By Diane Zak.
Project 1: Using Arrays and Manipulating Strings Essentials for Design JavaScript Level Two Michael Brooks.
מערכים (arrays) 02 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 15 1 Department of Computer Science-BGU.
Perl Chapter 5 Hashes. Outside of world of Perl, know as associative arrays Also called hash tables Perl one of few languages that has hashes built-in.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Perl COEN 351  Thomas Schwarz, S.J Perl Scripting Language Developed by Larry Wall 1987 to speed up system administration tasks. Design principles.
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
More About Data Types & Functions. General Program Structure #include statements for I/O, etc. #include's for class headers – function prototype statements.
Topic 3: Lists and arrays CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 3, pages 40-55, 86-91, Programming Perl 3rd edition.
Printing in Python. Printing Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external.
Introduction to LISP Atoms, Lists Math. LISP n LISt Processing n Function model –Program = function definition –Give arguments –Returns values n Mathematical.
Perl Scripting III Arrays and Hashes (Also known as Data Structures) Ed Lee & Suzi Lewis Genome Informatics.
2.1 Scalar data - revision numeric e-14 ( = 6.35 × )‏ operators: + (addition) - (subtraction) * (multiplication) / (division)
LISTS and TUPLES. Topics Sequences Introduction to Lists List Slicing Finding Items in Lists with the in Operator List Methods and Useful Built-in Functions.
Perl for Bioinformatics Part 2 Stuart Brown NYU School of Medicine.
Programming Perl in UNIX Course Number : CIT 370 Week 2 Prof. Daniel Chen.
Arrays and Lists. What is an Array? Arrays are linear data structures whose elements are referenced with subscripts. Just about all programming languages.
CSC 4630 Perl 2 adapted from R. E. Beck. I/O and Arithmetic Form pairs of programmer/investigator/discover Exercise 1. Write a program that prompts the.
Operators. Perl has MANY operators. –Covered in Chapter 3 of Camel –perldoc perlop Many operators have numeric and string version –remember Perl will.
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.
Introduction to Programming the WWW I CMSC Winter 2004 Lecture 8.
User-Written Functions
Containers and Lists CIS 40 – Introduction to Programming in Python
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong.
JavaScript: Functions.
Perl Variables: Array Web Programming.
Lists in Python.
With Assignment Operator
An Introduction to Perl
Context.
Perl I/O Learning Objectives:
Topics Sequences Introduction to Lists List Slicing
CHAPTER 4: Lists, Tuples and Dictionaries
Topics Sequences Introduction to Lists List Slicing
Presentation transcript:

Perl Arrays and Lists Learning Objectives: 1. To understand the format and the declaration of Arrays & Lists in Perl 2. To distinguish the difference between a List & an Array & their usage 3. To study the basic operations of Arrays & Lists in Perl

COMP111 Lecture 11 / Slide 2 Lists  A list is an ordered collection of scalar data.  A list begins and ends with parentheses, with the elements separated by commas (and optional spaces). (1,2, 3,4.1)  List elements can be constants or expressions: ("Bill", 4, "pie", "B. Gates") ($num, 17, $num+1+$i)  Memory for lists is dynamically allocated and removed as the program runs.

COMP111 Lecture 11 / Slide 3 Lists  The empty list (no elements) is represented by an empty pair of parenthesis: ( )# empty list  The list constructor “.. ” creates a list of values with increments of 1: (1.. 4) # same as (1, 2, 3, 4) (1, 5.. 7) # same as (1, 5, 6, 7) ($min.. $max) # depends on values of $min and $max ( ) # same as (1, 2, 3, 4, 5) ??!! (10.. 5) # same as ( ) -> it can’t count down

COMP111 Lecture 11 / Slide 4 Single-Word Lists  There is a shortcut for lists of single-word strings, the “ quote word ” function: ("bill", "gates", "pie", "toss") # usual version qw(bill gates pie toss) # same as above qw(bill gates pie toss) # also okay

COMP111 Lecture 11 / Slide 5 Arrays  An array contains a list (zero or more scalar values).  Array variable names are similar to scalar variable names, except the initial character is ” instead of “ $ = (1,2, copies = ("Bill", 4, "pie", "B. Gates"); $num = = ($num, 17, $num+1);  If a scalar value is assigned to an array variable, it becomes a single-element list = 4;# becomes (4) automatically

COMP111 Lecture 11 / Slide 6 Inserting Arrays  You can also insert array elements into = = (1, 10); # = # = 99); # (0,1,2,6,7,8,10,99)  Note that the inserted array elements are at the same level as the other elements, not in a “ sub-list ”.

COMP111 Lecture 11 / Slide 7 Left-Side Assignment  If a list only contains variables, you can use it on the left side of an assignment: ($a,$b,$c) = (1,2,3); # set $a=1, $b=2, $c=3 ($a,$b) = ($b,$a); # swap $a and $b = ($a,$b,$c); # set $d=$a # remove first element # and put it in $e # end up with: $a=2, $b=1, $c=3 # $d=2, $e) # no change  An array variable can only occur in the last position in the list, because the array variable is “ greedy ” and consumes all the remaining values.

COMP111 Lecture 11 / Slide 8 Array Length  If an array variable is assigned to a scalar variable, the number assigned is the length of the = (1,2,3); $n # $n gets 3, the length  The context determines whether the length of the array is used or the list: $n $n gets the length ($n) # $n gets the first element  The first assignment is a scalar assignment, is treated as a scalar, returning its length.  The second assignment is an array assignment, and gives the first element (silently discarding the rest).

COMP111 Lecture 11 / Slide 9 Array Subscripting  Each element of an array can be accessed by its integer position in the list.  The first element starts at position 0 (like C++ arrays).  The first element of array is accessed as = qw(bill gates pie toss); $name1 = $a[0]; # sets name1 to "bill" $name2 = $a[3]; # sets name2 to "toss" $a[1] = "clinton"; # a: qw(bill clinton pie toss)  Note that on the array name becomes a $ when accessing individual elements.

COMP111 Lecture 11 / Slide 10 Array Subscripting  You can use all the usual scalar operations on the array = (1,2,3); $a[0]++;# a: (2,2,3) $a[1] += 4;# a: (2,6,3) $a[2] += $a[0];# a: (2,6,5) # swap the first two elements ($a[0],$a[1]) = ($a[1],$a[0]); # a: (6,2,5)

COMP111 Lecture 11 / Slide 11 Array Slices  Accessing a list of elements from the same array is called a slice.  Perl provides a special shortcut for = # swap the first two # make all 3 elements like the = (7,4); # change the last two to 7 and 4 # a: (1,7,4)  Note that slices rather than $. This is because slices work with lists rather than scalar values.

COMP111 Lecture 11 / Slide 12 List Slices  Slices also work directly on lists: $c = (1,2,3,4,5)[2];# sets $c to = (1,2,3,4,5)[2,4];# to (3,5)  The second statement above is equivalent =

COMP111 Lecture 11 / Slide 13 “push” and “pop”  You can use push and pop to add and remove values from the end of an = (1,2,3); $new = 6; # same = # so far: (1,2,3,6) $oldvalue = # removes last element # so far: (1,2,3) # can push multiple values # so far: (1,2,3,4,5,6)  pop returns undef if given an empty array.

COMP111 Lecture 11 / Slide 14 “unshift” and “shift” an Array  You can use unshift and shift to add and remove values from the beginning of an = (1,2,3); $new = 6; # same = # so far: (6,1,2,3) $old = # removes first element # so far: (1,2,3) # can unshift multiple values # same = # so far: (4,5,6,1,2,3)  shift returns undef if given an empty array.

COMP111 Lecture 11 / Slide 15 “reverse” and “sort” an Array  The reverse function reverses the array, returning the resulting = = = reverse(1,2,3);# same  The sort function returns a sorted array in ascending ASCII = qw(small medium = large, medium, = sort(qw(small medium large)); # =

COMP111 Lecture 11 / Slide 16 Scalar and List Context  If an operator or function expects a scalar argument, the argument is evaluated in a scalar context. $n $n gets the length  If an operator or function expects a list argument, the argument is evaluated in a list context. ($n) # $n gets the first element  A scalar value used within a list context is promoted to a single-element = 1; = (1)

COMP111 Lecture 11 / Slide and $ (at sign) Refers to the entire array or slice of an array (when used with [ ]). $ (dollar sign) Refers to one element of the array, used with [ ]

COMP111 Lecture 11 / Slide 18 Array chomp  The chomp function also works for array variables, removing any ending newlines in each element = qw(unix\n shell\n perl); is now qw(unix shell perl)  Array chomp is especially useful when reading a file or input from a user.  in a list context will return all remaining lines up to the end of the file, or until the user hits = ;# read input in a list context remove all trailing newlines

COMP111 Lecture 11 / Slide 19 Example: Max Value  How to find the biggest value in an array of = (23,4,56,99,36,24); $max = $a[0]; for($i=1; $i++){ if($a[$i] > $max){ $max = $a[$i]; } print "max is: $max\n";