0

I need to store in Item of hashtable 2 int variables. But cannot undestand how to store it:

    $hash = @{}
    if($hash.contains($name) -eq $False){
        $value = 1
        $arr = @($my_var;$value)
        $hash.add($name,$arr)
    }else{
        $value = $hash.item($name)
        $value[1]++ #PROBLEM HERE
        $hash.item($name)[0] = $my_var
        $hash.item($name)[1] = $value
    }

Problem is that $value[1] not exists(Note1), $value[0] contains also $my_var and $value with space.

Note1 - The '++' operator works only on numbers. The operand is a 'System.Object[]'.

1 Answer 1

1

I'm not sure if I understand your question. Do you want to update the int-value? If so, your problem is that you overwrite the int-value that was stored in $hash.item($name)[1] with not just an int, but the whole array. You're essentially creating this:

$hash.item($name) = @($my_var, $value, @($my_var, $value+1))

You could fix this by updating this line:

$hash.item($name)[1] = $value

to

$hash.item($name)[1] = $value[1]

If $my_var is always the same value, you could clean it up by replacing the whole item:

$my_var = "test"
$name = "nametest"
#$hash = @{}   #ran this first time to create ht before commenting it out to avoid overwriting the ht

if($hash.Contains($name) -eq $False){
    $value = 1
    $arr = @($my_var;$value)
    $hash.add($name,$arr)
}else{
    $value = $hash.item($name)
    $value[1]++
    $hash.item($name) = $value
}
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.