Perl Chapter 4 Arrays Data types in Perl • Three – scalars, arrays and associative arrays (hashes) • Arrays more flexible in Perl – due to late binding of arrays to attributes – grow in length explicitly and can be made to shrink – types of elements may change • Perl provides a collection of functions and operators for common array manipulations Intro to Arrays • Linear data structure • Elements accessed through subscripted variables • Perl arrays store lists of scalar values – strings, numbers, or references • Arrays have no type – arrays can have all one type or any combination of the three types of values • Not defined to have any particular size List Literals • A list is an ordered sequence of values • Simplest list literals are lists of scalar literals, separated by commas, delimited with parentheses (5) ( ) (1, 5, 4, 3) (“abc”, “egf”, “ijk”) (“me”, 100, “you”, 50) • May also be scalar variables and expressions ($sum, “Sum”) (2*$total, “!”x20) – May be implicitly quoted qw(bob carol ted alice) List literals cont. • A range operator (ellipsis (..)) can be used to specify a range of scalar literals (0..6) (1.5..6) = (1.5, 2.5, 3.5, 4.5, 5.5) (5..3) = ( ) • A range operator also works for string elements – (‘a’..’z’) has 26 elements (“aa”..”zz”) has 676 elements (“aa”, “ab”, “ac”, …) Arrays • An array is a variable whose value is a list. • Variable names begin with an @ – separate namespace • • • • Array variables need not be declared Elements are scalar Array size is as long as it required to hold list An uninitialized array has ( ) as its value Assignment • An assignment to an array variable (no subscript) – results in array being assigned as a unit. @a = (2, 4, 6) @a = (‘a’, ‘b’, ‘c’, ‘d’) @a = (4.37) • If rhs of assignment is a scalar value it is converted to a singleton list. eg. @a = 1; @a is (1) • If rhs is a list literal that has more elements than target array size of target array is increased • if rhs has less excess elements are set to undef – eg. if @a is (‘a’, ‘b’, ‘c’, ‘d’) and then @a = (4); the last 3 elements are undef Array assignment • If an array is assigned to a scalar variable, its length is assigned to the target – eg. if @a is (2, 4, 6) then after $x = @a; assignment is performed, $x is 3 • A list literal that consists of only variables can be the target of an assignment ($a, $b)=(2, 4, 6); $a is 2 and $b is 4 – if fewer variables in target, excess ignored – if more variables in target, excess set to undef Array Assignment • if one of the elements of the target list is an array name, it consumes all of the rest of the rhs list. ($a, @list, $b) = (1, 2, 3, 4); $a is 1, @list is (2, 3, 4) and $b is undef – You can get the first element of a list and the rest of the elements of the list easily ($first, @rest)=@list; or ($first, @list)=@list; – if @list has only one element @rest = ( ) undef and empty • undef @list; list = ( ) an empty list • @list = undef; @list = (undef) NOT an empty list No multidimensional arrays in Perl, but they can be simulated with arrays that store lists of references (Ch. 5) Referencing array elements • use subscripts, delimited by brackets and $ – if array is @list, then $list[1] • 0 – origin index • subscripts are scalar valued expressions (coerced to integers if needed) • Note: no relationship between variable names @list and $list necessarily Example $list = “Darcie”; @list = (2, 4, 6, 8); $list[1] = 10; print “Scalar: $list Element: $list[1] List: @list”; Scalar: Darcie Element: 10 List: 2 10 6 8 • Array elements can appear in list literals (both as targets and rhs of assignments) ($list[0], $list[1])=($list[1], $list[0]); – works … list assignments are done in “parallel” • Negative subscripts – indexes from high end of array – - 1 is last element of array $# • $#list has the index of the last element of the list • can be used to set array to empty $#list = -1; • to prevent numerous resizing, can be used to preset size of an array – assign a value to expected highest subscript or to $#list $list[999] = 0; or $#list = 999; Slices • slice of an array is a reference to some subset of the elements of that array • begin with a @ (since they are arrays, too) @salaries = (34000, 41950, 52100, 39650); @salaries = (48500, 41500); #adds to end @first3_salaries = @salaries[0, 1, 2]; @next3_salaries = @salaries[3..5] Slices • subscripts in slices can be specified individually or if they are contiguous using a range operator • lists of subscripts can include expressions • slices can be specified for list literals • assume $first is 3 and $second is 1 @list = (1, 3, 5, 7, 9)[$first, $second]; @list is (7, 3) Slices • Two elements in array can be interchanged using slices @list = (2, 4, 6, 8); @list[1,3] = @list[3, 1]; @list is (2, 8, 6, 4) Printing arrays if array variable appears in a double quoted string, its value is interpolated prints values instead of variable with spaces between list elements @names=(“pooh”, “tigger”, “piglet”, rabbit”); print “the names are: @names \n”; The names are: pooh tigger piglet rabbit TRY @names=(“mary”, “jo”, “ann”); @names = sort @names; print “the names are: @names \n”; CTRL-Z
© Copyright 2026 Paperzz