1

I need to look for variables ending in 01 02 03 etc until the variable doesn't exist, then continue with the script. In this loop iteration 01 is repeated before continue to 02 03 04 05 etc. Why is 01 repeated?

My variables end in a double digits so I convert the integer to a string before looking for the variable. I've managed to work around the issue I'm having by incrementing $i again before entering the loop which is fine, but I'm a little perplexed.

Initial script that iterates 01 twice

$i=1
$CopyFiles_Content_No = ($i).ToString('00')
do {   
 $CopyFiles_Contents += (Get-Item env:$($CopyFiles_Step)_Contents$($CopyFiles_Content_No)).Value
 $CopyFiles_Content_No = ($i++).ToString('00')
} while (Test-Path env:$($CopyFiles_Step)_Contents$($CopyFiles_Content_No) -ErrorAction Ignore)

To replicate this easily just type the following into Powershell

$i=1     #Set Int
$CopyFiles_Content_No = ($i).ToString('00')     #Convert to double digit string
$CopyFiles_Content_No     #See output which is 01
$CopyFiles_Content_No = ($i++).ToString('00')     #Increment $i and assign to variable
$CopyFiles_Content_No     #See output which is still 01
$CopyFiles_Content_No = ($i++).ToString('00')     #Increment $i and assign to variable
$CopyFiles_Content_No     #See output which is 02
$CopyFiles_Content_No = ($i++).ToString('00')    #Increment $i and assign to variable
$CopyFiles_Content_No     #See output which is 03

Work around is to increment $1 before entering the loop

$i=1
$CopyFiles_Content_No = ($i++).ToString('00')
do {   
 $CopyFiles_Contents += (Get-Item env:$($CopyFiles_Step)_Contents$($CopyFiles_Content_No)).Value
 $CopyFiles_Content_No = ($i++).ToString('00')
} while (Test-Path env:$($CopyFiles_Step)_Contents$($CopyFiles_Content_No) -ErrorAction Ignore)
Bevan
  • 1,305
  • 1
  • 11
  • 17
  • 1
    It's a postincrement operator. It returns the old value. You might want to use `++$i` (preincrement). – Caramiriel May 20 '19 at 11:04
  • 3
    Simpler: `$i = 0; ($i++).ToString()`. Compare: `$i = 0; (++$i).ToString()`. This is immediately obvious to C programmers -- and incomprehensible to just about anyone else. For clarity, I recommend you actually use neither of these as an expression -- only use `$i`, and write `$i++` or `++$i` on its own line. There is no tax on lines. – Jeroen Mostert May 20 '19 at 11:04
  • The workaround is correct. – lit May 20 '19 at 14:40
  • @Caramiriel, that's it. Thanks! I can't recall ever seeing preincrement used in Powershell. I guess when the increment is on its own line `$i++` or `++$i` are interchangable. – Bevan May 21 '19 at 01:15
  • @JeroenMostert, I'm actually taxed 2 characters per new line `\n` once the code is run. If needed I can also remove the spaces around the operators and any indentations. To elaborate on your suggestion though, why wouldn't you use ++$i in an expression? Interested on the reasoning. – Bevan May 21 '19 at 01:18
  • Further to my previous comment, I'm restricted to 2000 characters :( – Bevan May 21 '19 at 01:38
  • @Caramiriel, please post as Answer so I can accept it as you were first to respond. – Bevan May 21 '19 at 01:39
  • Possible duplicate of [What is the difference between i++ and ++i?](https://stackoverflow.com/questions/3346450/what-is-the-difference-between-i-and-i) eventhough this is powershell – Caramiriel May 21 '19 at 07:36
  • 1
    @Bevan: if you're restricted to 2000 characters, what's with all the long variable names? And why `Get-Item` rather than `gi`? Your code is still way too readable. :-P On a more serious note, expressions with side effects in them make code harder to read and maintain -- it's way too easy to miss the fact that a variable is being modified, moving (parts of) expressions around to simplify calculations can break the code, and then of course there's the question of what side effects happen when -- exactly the reason you had to ask this question in the first place. A costly savings of characters. – Jeroen Mostert May 21 '19 at 10:12
  • Well explained! I'm in a bit of catch-22. I need to save characters but my code also needs to be readable/presentable to project teams and forms part of my documentation to projects. I also generally write so if I leave or get run over by a bus the next guy doesn't have to pull apart my code too much. I like your approach though so will alter accordingly. – Bevan May 22 '19 at 04:09
  • @Caramiriel, can you change your comment to an answer so I can accept? – Bevan Aug 29 '19 at 05:15

0 Answers0