Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing an ordered hashtable to a function

How can I go about passing an ordered hashtable to a function?

The following throws an error:

The ordered attribute can be specified only on a hash literal node.

function doStuff {
    Param (
        [ordered]$theOrderedHashtable
    )
    $theOrderedHashtable
}

$datFileWithMinSizes  = [ordered]@{"FileA.DAT" = "4"; "FileB.DAT" = "5"; "FileC.DAT" = "91" ; "FileD.DAT" = "847"  }

doStuff -theOrderedHashtable $datFileWithMinSizes

The following does not maintain the correct order:

function doStuff {
    Param (
        [Hashtable]$theOrderedHashtable = [ordered]@{}
    )
    $theOrderedHashtable
}

$datFileWithMinSizes  = [ordered]@{"FileA.DAT" = "4"; "FileB.DAT" = "5"; "FileC.DAT" = "91" ; "FileD.DAT" = "847"  }

doStuff -theOrderedHashtable $datFileWithMinSizes

The only way I can currently get this to work is by not specifying the type as follows, but I want to specify the type:

function doStuff {
    Param (
        $theOrderedHashtable
    )
    $theOrderedHashtable
}

$datFileWithMinSizes  = [ordered]@{"FileA.DAT" = "4"; "FileB.DAT" = "5"; "FileC.DAT" = "91" ; "FileD.DAT" = "847"  }

doStuff -theOrderedHashtable $datFileWithMinSizes
like image 248
Opononi Avatar asked Mar 03 '17 01:03

Opononi


People also ask

How do you pass a hash table as a parameter in PowerShell?

Instead, you can use PowerShell splatting. To splat a parameter set, first create a hashtable containing key/value pairs of each parameter and parameter argument. Then, once you have the hashtable built, pass that set of parameters to the command using @<hashtable name> .

How do I display a Hashtable in PowerShell?

To display a hash table that is saved in a variable, type the variable name. By default, a hash tables is displayed as a table with one column for keys and one for values. Hash tables have Keys and Values properties. Use dot notation to display all of the keys or all of the values.

What is the use of hash table in PowerShell?

Generally, you think of a hashtable as a key/value pair, where you provide one key and get one value. PowerShell allows you to provide an array of keys to get multiple values. In this example, I use the same lookup hashtable from above and provide three different array styles to get the matches.


1 Answers

Use the full type name:

function Do-Stuff {
    param(
        [System.Collections.Specialized.OrderedDictionary]$OrderedHashtable
    )
    $OrderedHashtable
}

To support both regular hashtables and ordered dictionaries, you'll have to use separate parameter sets: use the [System.Collections.IDictionary] interface, as suggested by briantist

function Do-Stuff {
    [CmdletBinding(DefaultParameterSetName='Ordered')]
    param(
        [Parameter(Mandatory=$true,Position=0,ParameterSetName='Ordered')]
        [System.Collections.Specialized.OrderedDictionary]$OrderedHashtable,

        [Parameter(Mandatory=$true,Position=0,ParameterSetName='Hashtable')]
        [hashtable]$Hashtable
    )
    if($PSCmdlet.ParameterSetName -eq 'Hashtable'){
        $OrderedHashtable = $Hashtable
    }
    $OrderedHashtable
}
like image 182
Mathias R. Jessen Avatar answered Oct 17 '22 05:10

Mathias R. Jessen