_learning perl_, chapter3 notes, 30.12.2001, nh def: LIST = ordered scalar data def: ARRAY = variable that holds a LIST | `-> ordered always starts with '@' its ELEMENTS are seperate scalars any # of elements list literal is the way you represent variables inside program: ^^^^^^^^^^^^ `-> in perens (4,$a,"fred",$x+3); -- reevaluated every time literal is used (1 .. 5); (6 .. 19,23,45); ($a .. $b); &c. instead of @list = ("a","b","c","d","etc",); @list = qw (a b c d etc); assignment: -------------> work with the = operators, &c. can call other arrays: @one = (1 .. 3); @two = ("six", @one, "seven") --> ("six",1,2,3,"seven") ^^^^ `-> *NOT* the list proper -- just a REFERENT to it! (pointer?) -- takes in the values, but does not actually contain the list. if a list only contains variable references, THE LIST LITERAL CAN ALSO BE TREATED AS A VARIABLE. ($a,$b,$c) = (1,2,3); ($a,$b) = ($b,$a); # swap $a and $b ($d,@fred) = ($a,$b,$c); # $d gets $a; @fred gets ($b,$c) ($e,@fred) = @fred; # $e gets @fred's 1st element # ($b), and fred is left with # only $c @fred = qw(nori mom alexis); $a = @fred; # $a = 3 (the LENGTH of the array) # -- using the array name in a SCALAR # CONTEXT ($a) = @fred; # $a gets @fred's first element, # "nori", because of perens INDEXING ARRAYS: arrays can be indexed numerically. @fred = (1,2,3); # $fred[0] would then be 1 ^^^ (index starts at 0, of course) ^ elements called with the $ operator, because it's not the whole array -------------------------- | elements of arrays | | take the same oper- | # $fred[0]++; | ators as normal | # $fred[1] *= 3; | scalars -- ++, *=, &c. | -------------------------- --> negative subscripts (index values) count backwards from end --> can also index with variables, i.e., $a=2; @fred[$a] # = @fred[2] def: a SLICE is accessing a list of elements from the same array: @fred = qw(nori mom alexis); # @fred[0,1] = "nori","mom" @fred[0,1] = @fred[1,0] # swaps $fred[0] with $fred[1] @fred[0,1,2] = @fred[1,1,1] # sets all 3 elements equal to "mom" ^ uses prefix '@' -- accessing part of array, not just scalar! --> $#fred gives the index value of the last element of @fred. def: POP pops values off the end of a pre-defined array. def: PUSH pushes values onto the end of a pre-defined array. `---------------------\/------------------------------' PUSH can take arguments: @list = (1 .. 3); push(@list,4,5,6); # @list = (1,2,3,4,5,6) $a = pop(@list); # $a = 6 SHIFT and UNSHIFT do the same things, just "to the 'left' side of a list (the portion with the lowest subscripts)" <-- what does that mean??? unshift(@fred,$a); # like @fred = ($a,@fred); $x = shift(@fred); # like ($x,@fred) = @fred def: REVERSE reverses the order of the elements in a list. @a = qw(red orange yellow) reverse(@a); # @a now = "yellow","orange","red" def: SORT sorts an array according to its ASCII value: 1,16,2,223,etc --> CHOMP works just as well on arrays as it does on plain scalars -- chomps each element as an array: ------------------- @a = ; * reads standard input in a list context * each line taken as element of an array * CTRL+D signals "end of file"