2

I would like to use the keys from an input hashtable ($htInput) in an output hashtable ($htOutput) without knowing the specific key names only their numeric position. Can I access the individual input hashtable keys using the $counter variable in the for loop:

$main = {
    Begin {
        Write-Host "SO Question Begin..." -ForegroundColor Black -BackgroundColor Green
    }
    Process {
        try {
            $htInput = @{Alpha = 1; Bravo = 2; Charlie = 3; Delta = 4; Echo = 5}
            $htOutput = @()
            for ($counter = 0; $counter -lt $htInput.Count; $counter++) {
                $rzlt = $rzlt + $counter
                $htOutput += $rzlt
            }
            # Expected Output: $htOutput = @{Alpha = 0; Bravo = 1; Charlie = 3; Delta = 6; Echo = 10}
            # Actual Output  : $htOutput = @(0; 1; 3; 6; 10)
            return $htOutput
        } catch {
            Write-Host "Error: $($_.Exception)" -ForegroundColor White -BackgroundColor Red 
            break
        }
    }
    End {
        if ($?) {
            Write-Host "SO Question End..." -ForegroundColor Black -BackgroundColor Green
        }
    }
}

& $main

The expected output is:

$htOutput = @{Alpha = 0; Bravo = 1; Charlie = 3; Delta = 6; Echo = 10}

but the actual output is:

$htOutput = @(0; 1; 3; 6; 10)

Please advise?

1 Answer 1

3

Feels like a homework. However, I see your sincere effort which I like. You get your result in Array - $htOutput = @() as you have defined the result to be array. You need to have it as Hash table - $htOutput = @{}.

Note: It is good to have all variables initialized. Second thing to note is that he sorting via .GetEnumerator can be influenced by your regional settings. Which can cause deviation from your wished result.

$main = {
    begin {
        Write-Host "SO Question Begin..." -ForegroundColor Black -BackgroundColor Green
    }
    process {
        try {
            $htInput = @{Alpha = 1; Bravo = 2; Charlie = 3; Delta = 4; Echo = 5}
            $htOutput = @{}
            $temp = 0
            $counter = 0
            ForEach ($item in $htInput.GetEnumerator()) {
                $temp = $temp + $counter
                $htOutput.Add($item.key,$temp)
                $counter++
            }
            # Expected Output: $htOutput = @{Alpha = 0; Bravo = 1; Charlie = 3; Delta = 6; Echo = 10}
            # Actual Output  : $htOutput = @(0; 1; 3; 6; 10)
            return $htOutput
        } 
        catch {
            Write-Host "Error: $($_.Exception)" -ForegroundColor White -BackgroundColor Red 
            break
        }
    }
    end {
        If ($?) {
            Write-Host "SO Question End..." -ForegroundColor Black -BackgroundColor Green
        }
    }
}

& $main
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.