Section A.3. Data Types


A.3. Data Types

MSH has a set of well-known data types that correspond to the basic types in the .NET Framework.

A.3.1. Numbers

Numbers can be represented as integers or double precision floating-point values. MSH will automatically convert a number to its appropriate integer or double format depending on the presence of a decimal point. Conversion between number formats can be achieved with the explicit casting syntax []:

 MSH> $a = 10                  # $a is an int MSH> $b = 3.14            # $b is a double MSH> $c = 0x20                # $c is an int (value 32 decimal) MSH> $d = [int]1.2            # $d is an int (value 1) MSH> $e = [double]3e2         # $e is a double (value 300.0)

MSH also supports the K, M, and G suffixes on integers to represent the kilo, mega, and giga multipliers of 210, 220, and 230, respectively:

 MSH> 2K 2048 MSH> 8M 8388608 MSH> 3G 3221225472

A.3.2. Strings

Strings can be defined either with single (') or double quotation marks ("). For a string in single quotes, the shell does no variable replacement and treats the string literally. For a string enclosed in double quotes, the shell performs variable expansion, trying to change any variable substrings into the values of the corresponding variables.

A.3.3. Arrays

An array can contain zero or more elements. Arrays are defined by separating the elements by commas. The empty array (one which is not null but contains no elements) is signified by @( ) notation.

All arrays allow their content to be retrieved by an indexer. Including a numerical index inside square brackets after an array will return a specific element or result in an out-of-range exception. Arrays also support the Count property, which indicates the number of elements present:

 MSH> $arr=1,2,3 MSH> $arr[1] 2 MSH> $arr=$(get-process) MSH> $arr.Count 48

A.3.4. Hashtables (Associative Arrays)

Hashtables are a special type of array that offers user-defined keys to identify their elements instead of a numerical index. Hashtables are defined by separating key=value pairs with semicolons inside curly braces. The empty hashtable is signified by the @{} notation.

Hashtables allow their content to be retrieved by the associative indexer (user-defined key). Like arrays, the key is included in square brackets to retrieve a corresponding value from a hashtable. Hashtables also include two collections, Keys and Values, that contain the set of keys and values, respectively:

 MSH> $ht= @{zeus = "andy"; apollo = "jack"} MSH> $ht["zeus"] andy MSH> $ht.Zeus andy MSH> $ht.Values andy jack




Monad Jumpstart
Monad Jumpstart
ISBN: N/A
EAN: N/A
Year: 2005
Pages: 117

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net