Search code examples
arrayspowershellhashtable

Trying to build a vocabulary trainer with a hashtable


Maybe this is the wrong approach but I try to build a simple vocabulary database using a Hashtable where I run into two problems.

First of all, if I have for several topics an own hashtable, can I list all the hashtables-names and how? Maybe in a loop? I was not able to figure out, to list the hashtables.

Secondly, I tried to put hashtables into a hashtable which somehow works but I still need to figure out if I am allowed to do so, or that is a wrong approach? Please have a look at the below examples.

(This is only for personal use and for me to learn more about Powershell but also a kind of a challenge to myself), any other suggestions are welcome.

I was trying around and experimenting but was not able to find any solution. I also tried to do something with Arrays but also no success. Maybe there is a solution with either Hashtables or arrays or there is even something better for such an approach?

$Vocabulary = @{

$Weekdays = [ordered]@{Monday = 'Montag';Tuesday = 'Dienstag';Wednesday = 'Mittwoch'}
$Months = [ordered]@{January = 'Januar';February = 'Februar';March = 'März'}

}

If possible I would like to be able to loop through all Hashtables within the Hashtable and receive their names. Using the above example, the output should be:

$Weekdays
$Months

I am able to find the keys and the values but to search for the name of the hashtable itself is my challenge here.


Solution

  • Your hashtable instantiation is using uninitialized variables for hashtable keys. If you do this:

    $Weekdays = [ordered]@{Monday = 'Montag';Tuesday = 'Dienstag';Wednesday = 'Mittwoch'}
    $Months = [ordered]@{January = 'Januar';February = 'Februar';March = 'März'}
    
    $Vocabulary = @{ "Weekdays" = $Weekdays, "Months"=$months }
    
    $Vocabulary.Keys
    

    You will get the desired output