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.

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

CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
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 Arrays and Lists Learning Objectives: 1. To understand the format and the declaration of Arrays & Lists in Perl 2. To distinguish the difference between.
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 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.
Loops – While, Do, For Repetition Statements Introduction to Arrays
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.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
2.1 Lists and Arrays Summary of 1 st lesson Single quoted and double quoted strings Backslash ( \ ) – the escape character: \t \n Operators:
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.
JaySummet IPRE Python Review 2. 2 Outline Compound Data Types: Strings, Tuples, Lists & Dictionaries Immutable types: Strings Tuples Accessing.
Expressions, Data Conversion, and Input
Lists in Python.
Handling Lists F. Duveau 16/12/11 Chapter 9.2. Objectives of the session: Tools: Everything will be done with the Python interpreter in the Terminal Learning.
CPTG286K Programming - Perl Chapter 3: Arrays and List Data.
Session 7 JavaScript/Jscript: Arrays Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
CS 330 Programming Languages 10 / 07 / 2008 Instructor: Michael Eckmann.
Built-in Data Structures in Python An Introduction.
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.
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.
Project 1: Using Arrays and Manipulating Strings Essentials for Design JavaScript Level Two Michael Brooks.
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.
CPTG286K Programming - Perl Chapter 5 & 6: Hashes & Basic I/O.
Topic 2: Working with scalars CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 2, pages 19-38, Programming Perl 3rd edition chapter.
Strings The Basics. Strings a collection data type can refer to a string variable as one variable or as many different components (characters) string.
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.
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.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
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.
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.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
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.
Containers and Lists CIS 40 – Introduction to Programming in Python
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong.
Perl Variables: Array Web Programming.
Lists in Python.
With Assignment Operator
An Introduction to Perl
Context.
String and Lists Dr. José M. Reyes Álamo.
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
Python Review
Presentation transcript:

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 the elements separated by commas (and optional spaces). (1,2, 3,4.1) l List elements can be constants or expressions: ("Bill", 4, "pie", "B. Gates") ($num, 17, $num+1+$i) l Memory for lists is dynamically allocated and removed as the program runs.

Slide 3 Lists l 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) ( ) # same as (1, 2, 3, 4, 5) ??!! (1, 5.. 7) # same as (1, 5, 6, 7) ($min.. $max) # depends on values of $min and $max (10.. 5) # same as ( ) -> it can’t count down

Slide 4 Single-Word Lists l 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

Slide 5 Arrays l 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); l If a scalar value is assigned to an array variable, it becomes a single-element list = 4;# becomes (4) automatically

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

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, 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.

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).

Slide 9 Array Subscripting l Each element of an array can be accessed by its integer position in the list. l 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.

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)

Slide 11 Array Slices l Accessing a list of elements from the same array is called a slice. l 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.

Slide 12 List Slices l 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) n The second statement above is equivalent =

Slide 13 Index Expressions l You can use expressions for the subscripts just like in = (5,6,7); $n = 2; $m = $list1[$n];# $m = 7 $p = $list1[$n-1];# $p = 6 ($p) = (5,6,7)[$n-1];# same thing using slice

Slide 14 Slice Expressions l You can use also array expressions to index slices if you want to be = = = # same # or ($nums[2], $nums[1], $nums[0]) # or (7,6,5)

Slide 15 Bad Subscripting If you access an array beyond the end of the array, the undef value is returned without warning. undef is 0 when used as a number, the empty string when used as a = (5,6,7); $nums[3] = "bill"; (5,6,7,"bill") $nums[5] = "g."; (5,6,7,"bill",undef,"g.") l Assignment to an array element with a subscript less than zero is a fatal error.

Slide 16 Backward Subscripting You can use $#bill to get the index value of the last element Accessing an array with a negative subscript counts back from the end. So, another way to get the last element is = qw(cheap rich lucky likespie); print $#bill;# prints 3 print $bill[$#bill];# prints likespie print $bill[$#bill-1];# prints lucky print $bill[-1];# prints likespie print $bill[-2];# prints lucky print $bill[-3];# prints rich

Slide 17 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.

Slide 18 unshift and shift 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.

Slide 19 reverse and sort 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)); # =

Slide 20 Array Variable Interpolation Array elements can be interpolated in double-quoted = qw(unix shell perl); print "$comp111[0] programming is mainly done "; print "in $comp111[2]\n"; $n=3; print "$comp111[0] programming is mainly done "; print "in $comp111[$n-1]\n";# same thing Arrays and array slices will be interpolated (printed) with spaces between the = (1,2,"bill",3); print prints a: 1 2 bill 3 print prints a1: 2 bill

Slide 21 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)

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 [ ]

Slide 23 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

Slide 24 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";