Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 4 down vote accepted

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 > 
share|improve this answer
    
Well that did it. –  user3258775 Jan 31 '14 at 20:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.