home *** CD-ROM | disk | FTP | other *** search
/ Chip 2008 June / CHIP-2008-06.iso / software / PowerShell / PowerShell_Setup_x86.msi / product.cab / about_Array.help_EN.txt < prev    next >
Encoding:
Text File  |  2007-10-29  |  7.9 KB  |  214 lines

  1. TOPIC
  2.     Arrays
  3.  
  4. SHORT DESCRIPTION
  5.     A compact data structure for storing data elements
  6.  
  7. LONG DESCRIPTION
  8.     An array is a data structure for storing a collection of data elements
  9.     of the same type. Windows PowerShell supports data elements, such as
  10.     string, int (32-bit integer), long (64-bit integer), bool (boolean), byte,
  11.     and other .NET object types. 
  12.  
  13. CREATING AND INITIALIZING AN ARRAY
  14.     To create and initialize an array, assign multiple values to a variable.
  15.     The values stored in the array are delimited with a comma and separated
  16.     from the variable name by the assignment operator (=). 
  17.  
  18.     For example, to create an array named $A that contains the seven
  19.     numeric (int) values: 22, 5, 10, 8, 12, 9, 80, type:
  20.  
  21.         $A = 22,5,10,8,12,9,80
  22.  
  23.     You can also create and initialize an array by using the range 
  24.     operator (..). For example, to create and initialize an array named 
  25.     "$B" that contains the values 5 through 8, type:
  26.  
  27.         $B = 5..8
  28.  
  29.     As a result, $B contains four values: 5, 6, 7, and 8.
  30.  
  31.     When no data type is specified, Windows PowerShell creates each array as
  32.     an object array (type: object[]). To determine the data type of an array, 
  33.     use the GetType() method. For example, to determine the data type of the
  34.     $a array, type:
  35.  
  36.         $a.gettype()
  37.  
  38.     To create a strongly typed array, that is, an array that can contain only
  39.     values of a particular type, cast the variable as an array type, such 
  40.     as string[], long[], or int32[]. To cast an array, precede the variable
  41.     name with an array type enclosed in bracket. For example, to create a
  42.     32-bit integer array named $ia containing four integers (1500, 2230, 3350,
  43.     and 4000), type:
  44.  
  45.         [int32[]]$ia = 1500,2230,3350,4000
  46.  
  47.     As a result, the $ia array can contain only integers.
  48.  
  49.     You can creating arrays that are cast to any supported type in the
  50.     .NET Framework. For example, the objects that Get-Process retrieves
  51.     to represent processes are of type System.Diagnostics.Process. To create
  52.     a strongly typed array of process objects, enter the following command:
  53.  
  54.         [Diagnostics.Process[]]$zz = Get-Process
  55.  
  56. READING AN ARRAY
  57.     You can refer to an array by using its variable name, such as $A or $a. 
  58.     Windows PowerShell is not case-sensitive.
  59.  
  60.     To display all elements in the array, type the array name. For example:
  61.  
  62.     $a
  63.  
  64.     You can refer to the elements in an array by using an index, beginning
  65.     at position 0. Enclose the index number in square brackets. For example,
  66.     to display the first element in the $a array, type:
  67.  
  68.         $a[0]
  69.  
  70.     To display the third element in the $a array, type:
  71.  
  72.         $a[2]
  73.  
  74.     Negative numbers count from the end of the array. For example, "-1" 
  75.     refers to the last element of the array. To display the last 3 elements
  76.     of the array, type:
  77.  
  78.         $a[-3..-1]
  79.  
  80.     However, be cautious when using this notation.
  81.  
  82.         $a[0..-2]
  83.  
  84.     does not refer to all the elements of the array, except for the last one.
  85.     It refers to the first, last, and second-to-last elements in the array.
  86.    
  87.     You can use the range operator to display a subset of all values in an 
  88.     array. For example, to display the data elements at index position 1 
  89.     through 3, type:
  90.  
  91.         $a[1..3]
  92.  
  93.     You can use the plus operator (+) to combine a range with a list of
  94.     elements in an array. For example, to display the elements at index
  95.     positions 0, 2, and 4 through 6, type:
  96.  
  97.         $a[0,2+4..6]
  98.  
  99.     To determine how many items are in an array, combine the range with the
  100.     length property of an array. For example, to display the elements from 
  101.     index position 2 to the end of the array, type:
  102.  
  103.         $a[2..($a.length-1)]
  104.  
  105.     The length is set to -1 because the index begins at position 0. Therefore,
  106.     in a three-element array (1,2,3), the index of the third element is 2, 
  107.     which is one less than the length of the array.
  108.  
  109.  
  110.     You can also use looping constructs, such as foreach, for, and while loops,
  111.     to refer to the elements in an array. For example, to use a foreach loop
  112.     to display the elements in the $a array, type: 
  113.  
  114.         foreach ($element in $a) {$element}
  115.  
  116.     The foreach loop iterates through the array and returns each value in
  117.     the array until reaching the end of the array.
  118.  
  119.     The for loop is useful when you are incrementing counters while examining
  120.     the elements in an array. For example, to return every other value in an
  121.     array by using a for loop, type:
  122.  
  123.         for ($i = 0; $i -le ($a.length - 1); $i += 2) {$a[$i]}
  124.  
  125.     You can use a while loop to display the elements in an array until a 
  126.     defined condition is no longer true. For example, to display the elements
  127.     in the $a array while the array index is less than 4, type:
  128.  
  129.         $i=0
  130.         while($i -lt 4) {$a[$i]; $i++}
  131.  
  132.     To learn about the properties and methods of an array, such as the Length 
  133.     property and the SetValue method, use the InputObject parameter of the
  134.     Get-Member cmdlet. When you pipe an array to Get-Member, it displays
  135.     information about the objects in the array. When you use the InputObject
  136.     parameter, it displays information about the array. 
  137.  
  138.     To find the properties and methods of the $a array, type:
  139.  
  140.     get-member -inputobject $a
  141.  
  142.  
  143. MANIPULATING AN ARRAY
  144.     You can change the elements in an array, add an element to an array, and
  145.     combine the values from two arrays into a third array.
  146.  
  147.     To change the value of a particular element in an array, specify the
  148.     array name and the index of the element that you want to change, and then
  149.     use the assignment operator (=) to specify a new value for the element. For
  150.     example, to change the value of the second item in the $a array (index
  151.     position 1) to 10, type:
  152.  
  153.         $a[1] = 10
  154.  
  155.     You can also use the SetValue method of an array to change a value. The
  156.     following example changes the second value (index position 1) of the $a
  157.     array to 500:
  158.  
  159.         $a.SetValue(500,1)
  160.  
  161.     You can append an element to an existing array by using the += operator.
  162.     This operator adds to an existing value. When the operator is used on an
  163.     element of an array, it increases the value of the element. When the
  164.     operator is used on the array itself, it appends the value. For example,
  165.     to append an element with a value of 200 to the $a array, type:
  166.  
  167.         $a += 200
  168.  
  169.     It is not easy to delete elements from an array, but you can create a new
  170.     array that contains only selected elements of an existing array. For
  171.     example, to create the $t array with all elements in the $a array, except
  172.     for the value at index position 2, type:
  173.  
  174.         $t = $a[0,1 + 3..($a.length - 1)]
  175.  
  176.     To combine two arrays into a single array, use the plus operator (+). The
  177.     following example creates two arrays, combines them, and then displays
  178.     the resulting combined array.
  179.  
  180.         $x = 1,3
  181.         $y = 5,9
  182.         $z = $x + $y
  183.  
  184.     As a result, the $z array contains 1, 3, 5, and 9.
  185.  
  186.     To delete an array, use the Remove-Item cmdlet to delete the variable that
  187.     contains the array. The following command specifies the element "a" in the
  188.     Variable: drive. 
  189.  
  190.         remove-item variable:a
  191.  
  192.     (For information about the Variable: drive, type 
  193.     "get-help variable-psprovider".)
  194.  
  195.  
  196. SEE ALSO
  197.     For information about associative arrays, enter the following command:
  198.  
  199.         help about_associative_array
  200.  
  201.     For information on casting variables, enter the following command:
  202.  
  203.         help about_assignment_operators
  204.  
  205.     For information about range operators, enter the following command:
  206.  
  207.         help about_operator
  208.  
  209.     For information on various looping constructs, enter any of the 
  210.     following commands:
  211.  
  212.         help about_for
  213.         help about_foreach
  214.         help about_while