IT Questions and Answers :)

Thursday, November 23, 2017

In PowerShell, all of the following are valid ways to create an array, except:

In PowerShell, all of the following are valid ways to create an array, except:

  • $array = %('this, 'that')
  • $array = @('this', 'that')
  • $array = ('this', 'that')
  • $array = [array]('this', 'that') 

In PowerShell, all of the following are valid ways to create an array, except:

EXPLANATION

In programming, arrays are collections of items.  In PowerShell, these items are always objects, and a single array can contain different, unrelated object types.
Typically, simply putting a comma between different items causes them to be treated together like an array.  You can also explicitly declare an array with the [array] type marker, or with the shorthand "@" symbol.
In PowerShell, the "%" symbol is an alias for "Foreach-Object", and the example above will throw an error.
Fun fact: in Perl, the "%" is used to declare a variable as a hashtable, which is similar to an array, but uses a named key index instead of an automatic number index; unlike in PowerShell, the variable prefix in Perl actually changes depending on use, so a hashtable variable would look like "%variable" instead of "$variable".
Share:

Popular Posts

Blog Archive