Perl: Arrays, Functions, References, Etc.

Slides:



Advertisements
Similar presentations
Advanced Perl WUC Perl Poetry Variable Scope.
Advertisements

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.
Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.
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.
Hash Tables1 Part E Hash Tables  
Scripting Languages Perl Chapter #4 Subroutines. Writing your own Functions Functions is a programming language serve tow purposes: –They allow you to.
I/O while ($line= ){ #remove new line char \n chomp($line); if($line eq “quit”){ exit(1); } while ( ){ #remove new line char \n chomp($_); if($_ eq “quit”){
Lecture 2 BNFO 135 Usman Roshan. Perl variables Scalar –Number –String Examples –$myname = “Roshan”; –$year = 2006;
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.
Subroutines. aka: user-defined functions, methods, procdures, sub-procedures, etc etc etc We’ll just say Subroutines. –“Functions” generally means built-in.
CSE S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc 1 Perl: Arrays, Functions, References, Etc. Arrays Slices, splices Contexts: list, scalar.
PERL Variables and data structures Andrew Emerson, High Performance Systems, CINECA.
subroutines and references
– Introduction to Perl 10/25/ Introduction to Perl - Recipes and Idioms Introduction to Perl Session 8 · recipes and.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Built-in Data Structures in Python An Introduction.
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
Hossain Shahriar Announcement and reminder! Tentative date for final exam need to be fixed! We have agreed so far that it can.
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.
1 More Perl Strings References Complex data structures –Multidimensional arrays Subprograms Perl OOP –Methods –Constructors and Instances –Inheritance.
Perl Chapter 6 Functions. Subprograms In Perl, all subprograms are functions – returns 0 or 1 value – although may have “side-effects” optional function.
Chapter 11: Perl Scripting Off Larry’s Wall. In this chapter … Background Terminology Syntax Variables Control Structures File Manipulation Regular Expressions.
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,
CPTG286K Programming - Perl Chapter 5 & 6: Hashes & Basic I/O.
A Few More Functions. One more quoting operator qw// Takes a space separated sequence of words, and returns a list of single-quoted words. –no interpolation.
 2001 Prentice Hall, Inc. All rights reserved. Chapter 7 - Introduction to Common Gateway Interface (CGI) Outline 7.1Introduction 7.2A Simple HTTP Transaction.
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
PERL By C. Shing ITEC Dept Radford University. Objectives Understand the history Understand constants and variables Understand operators Understand control.
Arrays and Lists. What is an Array? Arrays are linear data structures whose elements are referenced with subscripts. Just about all programming languages.
Perl References arrays and hashes can only contain scalars (numbers and strings)‏ if we want something more complicated (like an array of arrays) we use.
Chapter 17 Arrays Perl to denote an array, for = (10, 20, 30, 50); Array subscripts are number from 0. Array elements require scalar.
Perl Backticks Hashes printf Variables Pragmas. Backticks.
CSE 341 S. Tanimoto Perl Perl: User Defined Functions sub funny_print { my ($name, $age) print
Introduction to Perl: Part II
Arrays An array in PHP is an ordered map
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong.
JavaScript: Functions.
© 2013 Goodrich, Tamassia, Goldwasser
Perl: Functions, References, Etc.
Advanced Topics Web Programming.
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 3 Sandiway Fong.
Perl Variables: Array Web Programming.
Subroutines Web Programming.
Introduction to Perl Jarrad Battaglia.
Perl: Arrays, Functions, References, Etc.
Perl Practical Extraction and Report Language
Perl Variables: Hash Web Programming.
Perl: Arrays, Functions, References, Etc.
Context.
CGI II: Cookies & Stuff Web Programming.
Perl Functions.
Lesson 2. Control structures File IO - reading and writing Subroutines
Chapter 6 - Subroutines and Functions
Programming Perls* Objective: To introduce students to the perl language. Perl is a language for getting your job done. Making Easy Things Easy & Hard.
Perl: Arrays, Functions, References, Etc.
Chapter 5: Lists and Dictionaries
Scheme: Basic Functionality
Perl: Arrays, Functions, References, Etc.
Pointers and dynamic objects
Perl Practical Extraction and Report Language
Subroutines.
Useful functions.
Perl: Arrays, Functions, References, Etc.
CGI II: Cookies & Stuff Web Programming.
LING/C SC/PSYC 438/538 Lecture 5 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 3 Sandiway Fong.
Presentation transcript:

Perl: Arrays, Functions, References, Etc. Slices, splices Contexts: list, scalar Functions Calling forms Argument fetching Packages and Modules References: “intelligent pointers” Anonymous Variables (e.g., lists as values). CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc

CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc # Scalar assignment: $pi = 3.141; # Array assignment: @pies = ($pi, "22/7", "apple pi"); # Uses of an array: $r = 2; $area = $pies[0] * $r * $r; print 'The @pies array ', "is @pies.\n"; The @pies array is 3.141 22/7 apple pi. CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc

CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc Array Slices A slice of an array is a subsequence of its elements. # Array assignment: @primes = (2,3,5,7,11,13,17,19); # Assignment with slices: @low_primes = @primes(0..3); @misc_primes = @primes(1,3,5,7); # Interchanging two array elements: @low_primes(0,2) = @low_primes(2,0); # Assignment to a list of scalars: ($one, $two) = (1, 2, 3, 4); CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc

CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc Array Splices Splicing an array involves changing, inserting, or deleting elements of the array. @lucky = (1,2,3,4,5,6,7,8); $offset = 2; $length = 3; splice(@lucky, $offset, $length, (9,16,25)); print "Lucky numbers are @lucky.\n"; Lucky numbers are 1 2 9 16 25 6 7 8. CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc

Splicing to Remove Elements To remove elements when splicing, specify fewer new elements (or no replacement elements) than elements to be replaced. @lucky = (1,2,3,4,5,6,7,8); splice(@lucky, 2, 3); print "Lucky numbers are @lucky.\n"; Lucky numbers are 1 2 6 7 8. CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc

Splicing to Insert Elements To insert elements, use a length of 0 for the slice (or use a length less than the number of new elements). @lucky = (1,2,3,4,5,6,7,8); splice(@lucky, 2, 0, (9, 16, 25)); print "Lucky numbers are @lucky.\n"; Lucky numbers are 1 2 9 16 25 3 4 5 6 7 8. CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc

Splicing Near the End of an Array For an array @a we can use $#a for the index of the last element. We can also use -1. Also, -2 can be used to reference the second to last element, etc. @lucky = (1,2,3,4,5,6,7,8); print "The last 2 numbers ", "are $lucky[-2] and #lucky[$#a].\n"; The last 2 numbers are 7 and 8. CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc

List Context vs Scalar Context Any Perl function or operator can have behavior that depends on the "context" in which it is called. # Scalar context: $line = <INFILE>; # reads one line. # List context: @lines = <INFILE>; # reads all lines. CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc

CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc List Context List context is a calling situation in which a list is needed, as in an assignment to an array. If the function being called normally returns a scalar, the scalar will be converted to a list of one element. # List context: @list = 5; # Equivalent to $list[0] = 5; CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc

CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc Scalar Context Scalar context is a calling situation in which a scalar is needed, as in an assignment to a scalar variable. If the function being called normally returns a list, the last element of the list will be used as the scalar. $scalar = (1, 2, 3); # Equivalent to $scalar = 3; CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc

List and Scalar Contexts with print @primes = (2,3,5,7,11); print @primes; # List context 235711 print "@primes"; # Scalar context 2 3 5 7 11 print "Date: ", localtime(), "\n"; Date: 4737111689612591 print "Date: ", scalar localtime(), "\n"; Date: Wed Nov 28 13:55:01 2001 CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc

Perl: User Defined Functions sub funny_print { my ($name, $age) = @_; print <<END_FUNNY_PRINT; The person\’s name is $name, and the age is $age. END_FUNNY_PRINT return 1; } funny_print ("Abe", 50); # or &funny_print ("Abe", 50); # or funny_print "Abe", 50; # This last form is OK AFTER the defn. CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc

CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc Perl: Pronouns $_ # “It” (scalar default variable) @people = ("John", "Tran", "Pam"); foreach $person (@people) { print $person, " "; } foreach (@people) { print $_, " "; } print; print " "; } John Tran Pam John Tran Pam John Tran Pam CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc

CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc Perl: Pronouns @_ # “Them” (array default variable) funny_print(Abe, 191); sub funny_print { my ($name, $age) = @_; # my $name = $_[0]; my $age = $_[1]; ... } CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc

Fetching Function Args w/ shift sub add1 { my $number = shift; return $number + 1; } add1(5); # shift removes and returns the first # element of an array, and the default # array is @_ CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc

CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc Packages #!/usr/bin/perl -w Package OakTrees; sub getseeds { print "acorns"; } Package PalmTrees; sub getseeds { print "coconuts" ; } getseeds() ; # prints coconuts; OakTrees::getseeds(); # prints acorns PalmTrees::getseeds(): # prints coconuts # A package is a separate namespace for # functions and variables. # To cross packages use fully-qualified name CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc

CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc Modules #!/usr/bin/perl -w use MapleTreesModule; # The compiler searches for MapleTreesModule.pm # following all the paths in an array called @INC. # The module is a precompiled package of the same name. # When the “use” is executed, all the definitions in the # package become available, and any other code in # the package gets executed. # Packages can be in subdirectories of searched paths: use Net::Mail; # Mail.pm must be in Net. CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc

References (smart pointers) A reference is a pointer that is used to access some Perl data or function. There is a reference count for each item. $scalarref = \$scalar; $five = 5; $fiveref = \$five; print 1 + $$fiveref; # prints 6. CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc

References (continued) Local variables persist even if execution exits their scope if their reference count is greater than zero. (I.e., local variables can have indefinite extent.) References can be used for scalars, arrays, hashes, functions and other references. $scalar = "A Scalar"; $hash{"KEY"} = "A hash entry"; $scalarref = \$scalar; $hashref = \%hash; CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc

CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc Anonymous Variables sub someTrees { my @threeTrees = qw(oak pine elm); return \@threeTrees; } $treesRef = someTrees(); sub moreTrees { ["banyan", "beech"]; # anonymous # both functions return references. CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc