locked
How can I find a specific value in a hashtable RRS feed

  • Question

  • I accidentally posted this in the DPM forum.  I am trying to iterate through a hashtable and output the value based on the key.  I tried this:

     

    cls
    $hashtable = @{
    	"one" = 1;
    	"two" = 2;
    	"three" = 3;
    };
    foreach($key in $hashtable){
    	$key.Keys + ": " + $key.values
    }
    

    I want it to display this:
    one: 1
    two: 2
    three: 3
    

     

    But I get this instead:

     

    two
    three
    one
    :
    2
    3
    1
    Thursday, October 27, 2011 1:43 AM

Answers

  • Try this:

    cls
    $hashtable = @{
    	"one" = 1;
    	"two" = 2;
    	"three" = 3;
    };
    foreach($key in $hashtable.getenumerator()){
    	$key.Key + ": " + $key.value
    }
     
    
    

    For more help on hash tables:

    get-help about_hash_tables
    


    Paulo Morgado
    • Proposed as answer by Rich Prescott Thursday, October 27, 2011 2:23 AM
    • Marked as answer by Will Steele Friday, October 28, 2011 2:41 PM
    Thursday, October 27, 2011 2:05 AM
  • Because we are in powershell, you should consider using another aproach:

    $hashtable = @{
    	"one" = 1;
    	"two" = 2;
    	"three" = 3;
    };
    
    Write-Host 
    Write-Host "Standard aproach:"
    
    foreach($key in $hashtable.getenumerator()){
    	$key.Key + ": " + $key.value
    }
    
    Write-Host 
    Write-Host "Sorted by keys:"
    
    foreach($key in $($hashtable.Keys | sort)){
    	$key + ": " + $hashtable[$key]
    }
    
    Write-Host 
    Write-Host "A much flexible aproach (sort by key):"
    
    $hashtable.getenumerator() | sort key | foreach {
    	$_.Key + ": " + $_.value
    }
    
    Write-Host 
    Write-Host "A much flexible aproach (sort by value):"
    
    $hashtable.getenumerator() | sort value | foreach {
    	$_.Key + ": " + $_.value
    }
    

    • Marked as answer by Will Steele Friday, October 28, 2011 2:42 PM
    Friday, October 28, 2011 8:53 AM

All replies

  • Try this:

    cls
    $hashtable = @{
    	"one" = 1;
    	"two" = 2;
    	"three" = 3;
    };
    foreach($key in $hashtable.getenumerator()){
    	$key.Key + ": " + $key.value
    }
     
    
    

    For more help on hash tables:

    get-help about_hash_tables
    


    Paulo Morgado
    • Proposed as answer by Rich Prescott Thursday, October 27, 2011 2:23 AM
    • Marked as answer by Will Steele Friday, October 28, 2011 2:41 PM
    Thursday, October 27, 2011 2:05 AM
  • Just to add to this, items added to a hash table are not necessarily stored in the order they were added.  You have no control of the order when working with a hash table.
    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    Thursday, October 27, 2011 6:27 AM
  • Because we are in powershell, you should consider using another aproach:

    $hashtable = @{
    	"one" = 1;
    	"two" = 2;
    	"three" = 3;
    };
    
    Write-Host 
    Write-Host "Standard aproach:"
    
    foreach($key in $hashtable.getenumerator()){
    	$key.Key + ": " + $key.value
    }
    
    Write-Host 
    Write-Host "Sorted by keys:"
    
    foreach($key in $($hashtable.Keys | sort)){
    	$key + ": " + $hashtable[$key]
    }
    
    Write-Host 
    Write-Host "A much flexible aproach (sort by key):"
    
    $hashtable.getenumerator() | sort key | foreach {
    	$_.Key + ": " + $_.value
    }
    
    Write-Host 
    Write-Host "A much flexible aproach (sort by value):"
    
    $hashtable.getenumerator() | sort value | foreach {
    	$_.Key + ": " + $_.value
    }
    

    • Marked as answer by Will Steele Friday, October 28, 2011 2:42 PM
    Friday, October 28, 2011 8:53 AM