2

I'd like to create the variable offsetX, where X is some number. Right now, I use:

new-variable -name offset$i

where $i is an integer. However, what I really want is offset($i-1). How would I change the syntax of the above statement to accomplish this?

My latest attempt was:

new-variable -name offset+"[int]$i-1"

which didn't result in an error being thrown, but still doesn't accomplish my goal.

0

1 Answer 1

4

Put the subtraction part inside $(...), which is known as a SubExpression operator.

Below is a demonstration:

PS > $i = 2 
PS > New-Variable -Name offset$($i - 1) -Value value
PS > $offset1
value
PS > 
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.