If I have a hash table where the key is used in the value, e.g. 'hello' in this:
$ht = @{ 'hello' = 'hello world' }
Is it possible to reference the actual key in the value, something like this:
$ht = @{ 'hello' = "$key world" }
The real world example is a hash table where the keys are field names, and the values are script blocks that define how to compare the field between two objects, something like this:
$ht = @{
'thisField' = { Param($tp, $dp) $tp.thisField -eq $db.thisField }
'thatField' = { Param($tp, $dp) $tp.thatField -eq $db.thatField }
}
The comparison is often more complex than shown, and varies between fields, but each script block just compares the field that is the key for the block. I want to do something like this, if possible:
$ht = @{
'thisField' = { Param($tp, $dp) $tp.$key -eq $db.$key }
'thatField' = { Param($tp, $dp) $tp.$key -eq $db.$key }
}
Edit: Response to the question from @Mathias R. Jessen:
The real world hash table is used in test scripts. Testing database records is done by using a hash table where the keys are column names, and the script block defines how to verify the actual column value against the expected value. The comparison isn't always just '-eq'.
When testing a table, the hash table is used to verify that the expected records match the actual records, by comparing each field according to the script block, similar to this:
function compare($rules, $expected, $actual)
{
$rules.Keys | Foreach-Object {
$key = $_ # e.g. 'thisField'
$rule = $rules[$key] # e.g. { Param($tp, $dp) $tp.thisField -eq $db.thisField }
if (-not (& $rule $expected $actual))
{
throw "Comparison failed for $key"
}
}
Using hash tables with script blocks is a convenient way to define the rules for each table in a block of "data". It works well, I just wondered if there was a way to avoid repeating the column name.