DOC PREVIEW
FSU COP 4342 - COP 4342 Lecture Notes

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

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 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 22 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 22 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 22 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Accessing array elementsAccessing array elements in Perl is syntactically similar to C.Perhaps somewhat counterintuitively, you use $a[<num>] tospecify a scalar element of an array named @a.The index <num> is evaluated as a numeric expression.By default, the first index in an array is 0.Unix Tools: Perl 3Examples 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 elementUnix Tools: Perl 3Assign list literalsYou can assign a list literal to an array or to a list of scalars:($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 /; # dittoUnix Tools: Perl 3Array-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 4Unix Tools: Perl 3Printing entire arraysIf an array is simply printed, it comes out something like@a = (’a’,’b’,’c’,’d’);print @a;abcdIf an array is interpolated in a string, you get spaces: @a =(’a’,’b’,’c’,’d’); print "@a"; a b c dUnix Tools: Perl 3Arrays in a scalar contextGenerally, if you specify an array in a scalar context, the valuereturned 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 12Unix Tools: Perl 3Using a scalar in an array contextIf you assign an array a scalar value, that array will be just a oneelement 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!Unix Tools: Perl 3Size of arraysPerl arrays can be any size up to the amount of memory available forthe process. The number of elements can vary 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 undefUnix Tools: Perl 3Last element indexPerl has a special scalar form $#arrayname that returns a scalarvalue that is equal to the index of the last element in the array.for($i = 0; $i<=$#arr1; $i++){print "$arr1[$i]\n";}Unix Tools: Perl 3Last element index useYou can also use this special scalar form to truncate an array:@arr = (1..100); # arr has 100 elements...$#arr = 9; # now it has 10print "@arr";1 2 3 4 5 6 7 8 9 10Unix Tools: Perl 3Using negative array indicesA negative array index is treated as being relative to the end of thearray:@arr = 1..100;print $arr[-1]; # similar to using $arr[$#arr]100print $arr[-2];99Unix Tools: Perl 3Arrays as stacksArrays can be used as stacks, and Perl has built-ins that areuseful for manipulating arrays as stacks: push, pop,shift, and unshift.push takes two arguments: an array to push onto, and what is topushed on. If the new elment is an array, then the elements ofthat array are appended to the original array as scalars.A push puts the new element(s) at the end of the original array.A pop removes the last element from the array specified.Unix Tools: Perl 3Examples 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 valueUnix Tools: Perl 3shift and unshiftshift removes the first element from an arrayunshift inserts an element at the beginning of an arrayUnix Tools: Perl 3Examples 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’Unix Tools: Perl 3foreach control structureYou can use foreach to process each element of an array or list.It follows the form:foreach $SCAlAR (@ARRAY or LIST){<statement list>}(You can also map for similar processing.)Unix Tools: Perl 3foreach 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/;Unix Tools: Perl 3The default variable $_$_is the default variable (and is used in the previous map() examples).It is used as a default when at various times, such as when readinginput, writing output, and in the foreach and map constructions.Unix Tools: Perl 3The default variable $_while(<STDIN>){print;}$sum = 0;foreach(@arr){$sum += $_;}map { $sum += $_} @arr;Unix Tools: Perl 3Input from the “diamond” operatorReading from <> causes a program to read from the files specified onthe command line or stdin if no files are specified.Unix Tools: Perl 3Example 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.Unix Tools: Perl 3The @ARGV arrayThere is a builtin array called @ARGV which contains the commandlines arguments passed in by the calling program.Note that $ARGV[0] is the first argument, not the name of the Perlprogram being invokedUnix Tools: Perl


View Full Document

FSU COP 4342 - COP 4342 Lecture Notes

Download COP 4342 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 COP 4342 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 COP 4342 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?