CPTG286K Programming - Perl Chapter 3: Arrays and List Data.

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.
Ruby (on Rails) CSE 190M, Spring 2009 Week 2. Arrays Similar to PHP, Ruby arrays… – Are indexed by zero-based integer values – Store an assortment of.
Online Counseling Resource YCMOU ELearning Drive… School of Architecture, Science and Technology Yashwantrao Chavan Maharashtra Open University, Nashik.
1 Perl: arrays. 2 #!/usr/bin/perl -w # bind3.pl # # Here's an example that takes a unix path ($file) # and copies it to anothe variable ($filename) #
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.
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.
Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.
Lists Introduction to Computing Science and Programming I.
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.
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.
Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation.
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,
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Lists/Tuples. Example Write a program to keep track of all students’ scores on exam 1. Need a list of everyone’s score Use 14 doubles What about next.
CS 898N – Advanced World Wide Web Technologies Lecture 7: PERL Chin-Chih Chang
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
 Pearson Education, Inc. All rights reserved Arrays.
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.
Input/Output Chapters 7 & 9. Output n Print produces output > (print 100) n It also returns the value it printed –that’s where the second 100 came.
Perl Tutorial Presented by Pradeepsunder. Why PERL ???  Practical extraction and report language  Similar to shell script but lot easier and more powerful.
Data Structures in Python By: Christopher Todd. Lists in Python A list is a group of comma-separated values between square brackets. A list is a group.
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.
Print 'Hello world.'; Tren Griffith. Outline:  Perl introduction  Scalar Data  Variables  Operators  Control Structures  Input  Lists and Arrays.
Session 7 JavaScript/Jscript: Arrays Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5.
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
Chapter 8: Arrays.
– Introduction to Perl 10/25/ Introduction to Perl - Recipes and Idioms Introduction to Perl Session 8 · recipes and.
COMP519: Web Programming Autumn 2010 Perl Tutorial: The very beginning  A basic Perl Program The first line Comments and statements Simple printing 
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.
10. Python - Lists The list is a most versatile datatype available in Python, which can be written as a list of comma-separated values (items) between.
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
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.
Topic 4:Subroutines CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 4, pages 56-72, Programming Perl 3rd edition pages 80-83,
Copyright © Curt Hill Regular Expressions Providing a Search Pattern.
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.
(The Transpose Operator) 1 >> C=[ ; ; ] C = >> D=C' D =
Introduction to Perl NICOLE VECERE. Background General Purpose Language ◦ Procedural, Functional, and Object-oriented Developed for text manipulation.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
CHAPTER 6 ARRAYS IN C++ 2 nd Semester King Saud University College of Applied studies and Community Service CSC 1101 By: Fatimah Alakeel Edited.
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
Topic 3: Lists and arrays CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 3, pages 40-55, 86-91, Programming Perl 3rd edition.
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)
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.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
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.
Introduction to Programming the WWW I CMSC Winter 2004 Lecture 8.
Chapter 17 Arrays Perl to denote an array, for = (10, 20, 30, 50); Array subscripts are number from 0. Array elements require scalar.
String and Lists Dr. José M. Reyes Álamo.
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Perl Variables: Array Web Programming.
Programming Perls* Objective: To introduce students to the perl language. Perl is a language for getting your job done. Making Easy Things Easy & Hard.
String and Lists Dr. José M. Reyes Álamo.
Chapter 5: Lists and Dictionaries
Perl I/O Learning Objectives:
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Presentation transcript:

CPTG286K Programming - Perl Chapter 3: Arrays and List Data

Lists and Arrays A list is ordered scalar data An array variable holds a list Each array element is a scalar variable with an independent and ordered scalar value PERL arrays can have no elements, or can fill up to all of available memory

Literal Representation An empty list is represented by an empty pair of parentheses: () Non-empty lists contain comma-separated values enclosed in parentheses: (1,2,3)# array of 1, 2, and 3 (“fred”, 4.5)# array of “fred” and 4.5 List elements may contain expressions: ($a,17)# current value of $a and 17 ($b+$c,$d+$e)# two values from 4 variables

List Constructor Operator A list constructor operator creates a list of values List literals that contain two scalar values separated by two consecutive periods create a list of values Values start at left scalar value through the right scalar value, incrementing by one

Examples of List Constructor Operators (1..5)# same as (1, 2, 3, 4, 5) ( )# same as (1, 2, 3, 4, 5) ( )# same as (1, 2, 3, 4, 5, 6) (2..6,10,12)# same as (2,3,4,5,6,10,12) ($a..$b)# range determined by $a, $b # if $a > $b then empty list () (5..1)# same as () print (“The answer is # function print’s list literal

Array Variables Array variables hold a single list value Variable names for arrays are similar to those for scalars, except they start with rather than a $ The value of an un-assigned array variable is (), the empty is NOT related to $fred

Assigning Values Into Arrays Use the equal sign = to assign values into array variables Array variable names may appear in a list of literals (not same as list of lists); variable names are replaced by their current values If a scalar value is assigned to an array variable, the value becomes the single element of an array

Examples of Assigning = is Copied to barney = 1;# Scalar 1 is single # element value of = qw(one is = barney becomes # = = # (8,4,5,“one”,“two”,6,7,“last”)

Variable References If a list literal contains only variable references such as ($a, $b, $c), rather than expressions such as ($a+$b, $c-1, $d*$z), the references can be treated as variables If number of elements does not match number of variables, excess values on the right side of equal sign are discarded; excess variables are assigned undef

Intro to Scalar and Array Context Scalar context: a scalar variable is assigned an array variable. The scalar variable yields the array’s = (4,5,6);# $a $a gets 3, the length Array (or list) context: an array variable receives another array variable: ($a) $a gets first element

Examples of Variable References ($a,$b,$c) = (1,2,3);# assign 1,2,3 to $a, $b, $c ($a,$b) = ($b,$a);# swap with no tmp variable! = ($a,$b,$c);# $d = = ($b,$c) to $e, so # $e = = ($c) Note: Place the array variable at the end of the list, because it may contain an undetermined number of = (4,5,6);# $a scalar context: $a on left # $a gets 3, length ($a) array context: ($a) on left # $a gets first element

Array Element Access Elements are accessed as scalar variables; the first element is accessed as $fred[0] A negative subscript counts elements from the end: -1 is last element, second to last is -2, etc. $#array_name yields the index value of the last element

Array Element Access = (7,8,9);# $a = 2; $b = $fred[0];# $b is 7 $b = $fred[$a];# same as $fred[2], $b is now $fred[0] = = (5,8,9); $c = $fred[1];# $c is 8 $fred[2]++;# increment third element $fred[1] += 4;# add 4 to second element ($fred[0],$fred[1]) = ($fred[1],$fred[0]); # swap the first two = (“fred”, “wilma”, “pebbles”, “dino”); print $fred[-1];# prints “dino” print $#fred;# prints 3 print $fred[$#fred];# prints “dino” too

Array Element Access Using A

Slice access Use prefix instead of $, because you’re accessing a part of an array, which is more than just one scalar same as # swap first two # make all 3 elements like the = (9,10); # change last two values to 9 and 10 $a = 2;# set $a to 2 ($c) = (7,8,9)[$a-1];# $c = 8

Examples of Slice or # swap first two # make all 3 elements like the = (9,10);# change last two values # to 9 and = (7,8,9);# ($c) = (7,8,9)[$a-1];# same as $fred[1], $c = = (2,1,0);# Setup subscript = # or (9,8,7)

Out of range access Assigning a value beyond the end of array, automatically extends it and assigns undef to existing intermediate = (1,2,3, “ hi”);# $fred[6] = “ho”; is #(1,2,3,“hi”, undef, undef, “ho”) An undef value is returned for array elements accessed beyond the end of = $fred[7]; # $barney is undef

Push and Unshift functions The push function adds elements to the end of the list; the unshift function adds elements to the beginning of the = is now # (8,’eight’,9,’nine’,10,’ten’); Big Seven’); # (‘The Big Seven’,8,’eight’,9,’nine’,10,’ten’);

Pop and Shift functions The pop function removes elements from the end of list; the shift function removes elements from the beginning of = (8,’eight’,9,’nine’,10); is now # (8,’eight’,9,’nine’); $removed = shift # $removed = 8 # #somelist is now # (’eight’,9,’nine’);

Reverse function The reverse function reverses the order of elements in the = is (9,8,7) is unchanged The argument list is unaltered by the function unless it is assigned to the same = is reverse of itself

Sort function The sort function sorts its arguments as single strings in ascending ASCII order A sorted list is returned without altering the original = sort(“small”, “medium”, “large”); is “large”, “medium”, = = performs ASCII sort is 1,16,2,32,4,64,8

Chomp and STDIN in list context The chomp function also works on array variables to remove the newline character from each array = (“hello\n”,”world\n”,”happy days”); is now (“hello”,”world”,”happy days”) in list context can read remaining lines up to end of = ;# read until EOF, or CTRL-D (Z)

Variable Interpolation in Arrays Arrays may be interpolated into double- quoted strings Use the curly bracket delimitter to distinguish scalar variable placed next to square brackets, from array variables Portions of an array can be interpolated by using slices

Examples of Array = (“hello”, “dolly”); $y = 2; $x = “This is $fred[$y-1]’s place”; # This is dolly’s place $fred = “right”; $y = “This is $fred[1]”; # $y=“This is dolly” $y = “This is ${fred}[1]”; # “This is = = “Now here!”; # $all gets “Now for ccc 1 = “Now for here!”; # same