DOC PREVIEW
FSU COP 4342 - Lecture Notes

This preview shows page 1-2-22-23 out of 23 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Fall 2006 Perl 03Accessing array elements☞ Accessing array elements in Perl is syntactically similarto C.☞ Perhaps somewhat counterintuitively, you use$a[<num>] to specify a scalar element of an arraynamed @a.☞ The index <num> is evaluated as a numeric expression.☞ By default, the first index in an array is 0.COP 4342Fall 2006 Pe rl 03Examples of arracy access$a[0] = 1; # assign numeric constant$a[1] = "string"; # assign string constantprint $m[$a]; # access via variable$a[$c] = $b[$d]; # copy elements$a[$i] = $b[$i]; #$a[$i+$j] = 0; # expressions are okay$a[$i]++; # increment elementCOP 4342Fall 2006 Pe rl 03Assign list literalsYou can assign a list literal to an array or to a list ofscalars:($a, $b, $c) = (1, 2, 3); # $a = 1, $b = 2, $c = 3($m, $n) = ($n, $m); # works!@nums = (1..10); # $nums[0]=1, $nums[1]=2, ...($x,$y,$z) = (1,2) # $x=1, $y=2, $z is undef@t = (); # t is defined with no elements($a[1],$a[0])=($a[0],$a[1]); # swap works!@kudomono = (’apple’,’orange’); # list with 2 elements@kudomono = qw/ apple orange /; # dittoCOP 4342Fall 2006 Pe rl 03Array-wide accessSometimes you can do an operation on an entire array.Use the @array name:@x = @y; # copy array y to x@y = 1..1000; # parentheses are not requisite@lines = <STDIN> # very useful!print @lines; # works in Perl 5, not 4COP 4342Fall 2006 Pe rl 03Printing entire arrays☞ If an array is simply printed, it comes out somethinglike@a = (’a’,’b’,’c’,’d’);print @a;abcd☞ If an array is interpolated in a string, you get spaces:@a = (’a’,’b’,’c’,’d’); print ”@a”; a b c dCOP 4342Fall 2006 Pe rl 03Arrays in a scalar contextGenerally, if you specify an array in a scalar context,the value returned is the number of elements in the array.@array1 = (’a’, 3, ’b’, 4, ’c’, 5); # assign array1 the values of list@array2 = @array1; # assign array2 the values of array1$m = @array2; # $m now has value 6$n = $m + @array1 # $n now has value 12COP 4342Fall 2006 Pe rl 03Using a scalar in an array contextIf you assign an array a scalar value, that array will bejust a one element array:$m = 1;@arr = $m; # @arr == ( 1 );@yup = "apple"; # @yup == ( "apple" );@arr = ( undef ); # @arr == ( undef );@arr = (); # @arr is now empty, not an array with one undef value!COP 4342Fall 2006 Pe rl 03Size of arraysPerl arrays can be any size up to the amount of me moryavailable for the process. The number of elements canvary during execution.my @fruit; # has zero elements$fruit[0] = "apple"; # now has one element$fruit[1] = "orange"; # now has two elements$frist[99] = ’plum’; # now has 100 elements, most of which are undefCOP 4342Fall 2006 Pe rl 03Last element indexPerl has a special scalar form $#arrayname that returnsa scalar value that is equal to the index of the last elementin the array.for($i = 0; $i<=$#arr1; $i++){print "$arr1[$i]\n";}COP 4342Fall 2006 Pe rl 03Last element index useYou c an also use this special scalar form to truncate anarray:@arr = (1..100); # arr has 100 elements...$#arr = 9; # now it has 10print "@arr";1 2 3 4 5 6 7 8 9 10COP 4342Fall 2006 Pe rl 03Using negative array indicesA negative array index is treated as being relative tothe end of the array:@arr = 1..100;print $arr[-1]; # similar to using $arr[$#arr]100print $arr[-2];99COP 4342Fall 2006 Pe rl 03Arrays as stacks☞ Arrays can be used as stacks, and Perl has built-insthat are useful for manipulating arrays as stacks: push,pop, shift, and unshift.☞ push takes two arguments: an array to push onto, andwhat is to pushed on. If the new elment is an array,then the elements of that array are appended to theoriginal array as sc alars.COP 4342Fall 2006 Pe rl 03☞ A push puts the new element(s) at the end of theoriginal array.☞ A pop removes the last element from the arrayspecified.COP 4342Fall 2006 Pe rl 03Examples of push and poppush @nums, $i;push @ans, "yes";push @a, 1..5;push @a, @b; # appends the elements of b to apush @a, (1, 3, 5);pop @a;push(@a,pop(@b)); # moves the last element of b to end of a@a = (); @b = (); push(@b,pop(@a)) # b now has one undef valueCOP 4342Fall 2006 Pe rl 03shift and unshift☞ shift rem oves the first element from an array☞ unshift inserts an element at the beginning of anarrayCOP 4342Fall 2006 Pe rl 03Examples of shift and unshift@a = 1..10;unshift @a,99; # now @a == (99,1,2,3,4,5,6,7,8,9)unshift @a,(’a’,’b’) # now @a == (’a’,’b’,99,1,2,3,4,5,6,7,8,9)$x = shift @a; # now $x == ’a’COP 4342Fall 2006 Pe rl 03foreach control structureYou can use foreach to proces s each elem ent of anarray or list.It follows the form:foreach $SCAlAR (@ARRAY or LIST){<statement list>}(You can also map for similar processing.)COP 4342Fall 2006 Pe rl 03foreach examplesforeach $a (@a){print "$a\n";}map {print "$_\n";} @a;foreach $item (qw/ apple pear lemon /){push @fruits,$item;}map {push @fruits, $_} qw/ apple pear lemon/;COP 4342Fall 2006 Pe rl 03The default variable $$ is the default variable (and is used in the previousmap() examples). It is used as a default when at varioustimes, such as when reading input, writing output, and inthe foreach and map constructions.COP 4342Fall 2006 Pe rl 03The default variable $while(<STDIN>){print;}$sum = 0;foreach(@arr){$sum += $_;}map { $sum += $_} @arr;COP 4342Fall 2006 Pe rl 03Input from the “diamond” operatorReading from <> causes a program to read from thefiles specifie d on the command line or stdin if no files arespecified.COP 4342Fall 2006 Pe rl 03Example of diamond operator#!/usr/bin/perl -w# 2006 09 22 - rdl script23.plwhile(<>){print;}You can either use ./Script23.pl < /etc/hosts or./Script23.pl /etc/hosts /etc/resolv.conf.COP 4342Fall 2006 Pe rl 03The @ARGV arrayThere is a builtin array called @ARGV which containsthe command lines arguments passed in by the callingprogram.Note that $ARGV[0] is the first argument, not the nameof the Perl program being invokedCOP


View Full Document

FSU COP 4342 - Lecture Notes

Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?