Perl Arrays Functions References Etc Arrays Slices splices

  • Slides: 20
Download presentation
Perl: Arrays, Functions, References, Etc. Arrays Slices, splices Contexts: list, scalar Functions Calling forms

Perl: Arrays, Functions, References, Etc. Arrays 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, 1

Arrays # Scalar assignment: $pi = 3. 141; # Array assignment: @pies = ($pi,

Arrays # Scalar assignment: $pi = 3. 141; # Array assignment: @pies = ($pi, "22/7", "apple pi"); # Uses of an array: $r = 2; $area = $pies[0] * $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, 2

Array Slices A slice of an array is a subsequence of its elements. #

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

Array Splices Splicing an array involves changing, inserting, or deleting elements of the array.

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

Splicing to Remove Elements To remove elements when splicing, specify fewer new elements (or

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

Splicing to Insert Elements To insert elements, use a length of 0 for the

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

Splicing Near the End of an Array For an array @a we can use

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

List Context vs Scalar Context Any Perl function or operator can have behavior that

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

List Context List context is a calling situation in which a list is needed,

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

Scalar Context Scalar context is a calling situation in which a scalar is needed,

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

List and Scalar Contexts with print @primes = (2, 3, 5, 7, 11); print

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

Perl: User Defined Functions sub funny_print { my ($name, $age) = @_; print <<END_FUNNY_PRINT;

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

Perl: Pronouns $_ # “It” (scalar default variable) @people = ("John", "Tran", "Pam"); foreach

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

Perl: Pronouns @_ # “Them” (array default variable) funny_print(Abe, 191); sub funny_print { my

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

Fetching Function Args w/ shift sub add 1 { my $number = shift; return

Fetching Function Args w/ shift sub add 1 { my $number = shift; return $number + 1; } add 1(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, 15

Packages #!/usr/bin/perl -w Package Oak. Trees; sub getseeds { print "acorns"; } Package Palm.

Packages #!/usr/bin/perl -w Package Oak. Trees; sub getseeds { print "acorns"; } Package Palm. Trees; sub getseeds { print "coconuts" ; } getseeds() ; # prints coconuts; Oak. Trees: : getseeds(); # prints acorns Palm. Trees: : 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, 16

Modules #!/usr/bin/perl -w use Maple. Trees. Module; # The compiler searches for Maple. Trees.

Modules #!/usr/bin/perl -w use Maple. Trees. Module; # The compiler searches for Maple. Trees. Module. 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, 17

References (smart pointers) A reference is a pointer that is used to access some

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

References (continued) Local variables persist even if execution exits their scope if their reference

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

Anonymous Variables sub some. Trees { my @three. Trees = qw(oak pine elm); return

Anonymous Variables sub some. Trees { my @three. Trees = qw(oak pine elm); return @three. Trees; } $trees. Ref = some. Trees(); sub more. Trees { ["banyan", "beech"]; # anonymous } # both functions return references. CSE 341 -- S. Tanimoto Perl Arrays, Functions, Lists, Refs, 20