`

Note of Learning Perl--Lists and Arrays

    博客分类:
  • Perl
阅读更多

Lists and Arrays
------------------

1. Lists and Arrays
 1) A list is an ordered collection of scalars. An array is a variable that contains a list. In Perl, the two terms are often used as if they're interchangeable. But, to be accurate, the list is the data, and the array is the variable. You can have a list value that isn't in an array, but every array variable holds a list.
 2) The subscript may be any expression that gives a numeric value. If it's not an integer already, it'll automatically be truncated to the next lower integer.
 3) If the subscript indicates an element that would be beyond the end of the array, the corresponding value will be undef. This is just as with ordinary scalars; if you've never stored a value into the variable, it's undef.
 
2. Special Array Indics
 1) If you store into an array element that is beyond the end of the array, the array is automatically extended as needed -- there's no limit on its length, as long as there's available memory for Perl to use. If intervening elements need to be created, they'll be created as undef values.
 2) If you store into an array element that is beyond the end of the array, the array is automatically extended as needed -- there's no limit on its length, as long as there's available memory for Perl to use. If intervening elements need to be created, they'll be created as undef values.
 3) The last element index of an array(e.g the variable name of the array is rocks) is $#rocks
 4) Perl provide a shortcut: negative array indices count from the end of the array. If you've got three elements in the array, the valid negative indices are -1 (the last element), -2 (the middle element), and -3 (the first element).
 
3. List Literals
 1) qw stands for "quoted words" or "quoted by whitespace," depending upon whom you ask. Either way, Perl treats it like a single-quoted string (so, you can't use \n or $fred inside a qw list as you would in a double-quoted string). The whitespace (characters like spaces, tabs, and newlines) will be discarded, and whatever is left becomes the list of items.
 
4. List Assignment
 1) In much the same way as scalar values may be assigned to variables, list values may also be assigned to variables: ($fred, $barney, $dino) = ("flintstone", "rubble", undef);
 2) Since the list is built up before the assignment starts, this makes it easy to swap two variables' values in Perl: ($fred, $barney) = ($barney, $fred);
 3) In a list assignment, extra values are silently ignored -- Perl figures that if you wanted those values stored somewhere, you would have told it where to store them. Alternatively, if you have too many variables, the extras get the value undef.
 4) Just use the at-sign (@) before the name of the array (and no index brackets after it) to refer to the entire array at once.
 5) The value of an array variable that has not yet been assigned is ( ), the empty list. Just as new, empty scalars start out with undef, new, empty arrays start out with the empty list.
 
5. Interpolating Arrays into Strings
 1) Like scalars, array values may be interpolated into a double-quoted string. Elements of an array are automatically separated by spaces upon interpolation.
 2) A single element of an array will be replaced by its value, just as you'd expect:
  @fred = qw(hello dolly);
  $y = 2;
  $x = "This is $fred[1]'s place";    # "This is dolly's place"
  $x = "This is $fred[$y-1]'s place"; # same thing
  Note that the index expression is evaluated as an ordinary expression, as if it were outside a string. It is not variable-interpolated first. In other words, if $y contains the string "2*4", we're still talking about element 1, not element 7, because "2*4" as a number (the value of $y used in a numeric expression) is just plain 2.
 3) If you want to follow a simple scalar variable with a left square bracket, you need to delimit the square bracket so that it isn't considered part of an array reference, as follows:
  @fred = qw(eating rocks is wrong);
  $fred = "right";               # we are trying to say "this is right[3]"
  print "this is $fred[3]\n";    # prints "wrong" using $fred[3]
  print "this is ${fred}[3]\n";  # prints "right" (protected by braces)
  print "this is $fred"."[3]\n"; # right again (different string)
  print "this is $fred\[3]\n";   # right again (backslash hides it
  
6. The foreach Control Structure
 1) The control variable is not a copy of the list element -- it actually is the list element. That is, if you modify the control variable inside the loop, you'll be modifying the element itself.
 2) What is the value of the control variable after the loop has finished? It's the same as it was before the loop started. The value of the control variable of a foreach loop is automatically saved and restored by Perl. While the loop is running, there's no way to access or alter that saved value. So after the loop is done, the variable has the value it had before the loop, or undef if it hadn't had a value. That means that if you want to name your loop control variable "$rock", you don't have to worry that maybe you've already used that name for another variable, unless the variable name has been declared as a lexical in the current scope, in which case you get a lexically local variable instead of a package local.
 
7. Perl's Favorite Default: $_
 1) If you omit the control variable from the beginning of the foreach loop, Perl uses its favorite default variable, $_. This is (mostly) just like any other scalar variable, except for its unusual name.
 2) Remember that reverse operator returns the reversed list; it doesn't affect its arguments. If the return value isn't assigned anywhere, it's useless.
 3) The sort operator sorts a list of values in the internal character ordering.  And like what happened with reverse, the arguments themselves aren't affected. If you want to sort an array, you must store the result back into that array.
 
8. <STDIN> in List Context
 1) <STDIN> returns the next line of input in a scalar context. Now, in list context, this operator returns all of the remaining lines up to the end of file. Each line is returned as a separate element of the list.
 2)It turns out that if you give chomp a list of lines, it will remove the newlines from each item in the list.

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics