S
S
SanDn2020-02-28 12:29:54
PowerShell
SanDn, 2020-02-28 12:29:54

How to implement search in a multilevel hash table?

Here is an example of such a table.

$aa = @{"key1" = @{"key2" = @{"key3" = @{"key4" = "value4"}}}}

How to get to "value4" without manually tapping $aa.key1.key2.key3.key4 etc.
I would like to pass the desired value to a certain function or cmdlet and get a link to it in the table.
Well, take into account at the same time that nested elements can also be [array] or even [pscustomobject]
I suspect that this question is as old as the world, and ready-made solutions are enough. But due to my inexperience
, I can not find anything, so excuse me.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MaxKozlov, 2020-02-28
@MaxKozlov

Without support for arrays, I once wrote for myself such a recursive function

function Get-ObjectValue($Object, $PropertyName) {
  $property, $remain  = $PropertyName -split '\.',2
  if ($remain) {
    Get-ObjectValue $Object.$property $remain
  }
  else {
    $Object.$property
  }
}

The array needs to be added

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question