2

I'm moving my c# program over to Powershell, and I'm stuck with trying to convert my for loop I had working. I don't know quiet how to word this, but I require multiple counter variables within my for loop, and I haven't been able to figure out how to do it. I also couldn't find any references online, so I don't know if this is a limitation of Powershell or not.

So, here's what I mean, in c# I have:

  for (int k = 1, file = 1, perFile = 0; k <= splitGroups; k++, file++, perFile += dtotalPerFile){

        }

of course the variable names/types don't matter, so my question is how can I reconstruct this into Powershell? Is it possible to use a For loop? or do I need to move to perhaps a do while to achieve the same results.

4
  • You do realize that the for loop you're describing does absolutely nothing? Commented Nov 21, 2015 at 0:27
  • 2
    You do realize that's not the point of the question, there is a nested loop inside of this one, but that is beyond the scope of this question. I'm just looking to figure out how to format this in Powershell I did state the variable names did NOT matter. Commented Nov 21, 2015 at 0:28
  • It might also be worthwhile mentioning what the loop is actually achieving. It might be there's a powershell cmdlet that can do what you're after without translating directly as a for loop Commented Nov 21, 2015 at 0:30
  • 2
    I do, but I'm wondering if there's a more PowerShell-idiomatic way to do what you're describing Commented Nov 21, 2015 at 0:31

1 Answer 1

5

PowerShell's for loop can do this, but the syntax might be a bit awkward.

You need to wrap the multiple statements for each "section" of the for loop in $(), so it would look something like this:

for ( $($k = 1 ; $file = 1 ; $perFile = 0) ; $k -le $splitGroups; $( $k++ ; $file++ ; $perFile += $dtotalPerFile) ) {
    # stuff
}

for loops do allow newlines instead of semi-colons, and the sub-expressions do too, so you can clean it up a bit:

for ( 
    $(
        $k = 1 
        $file = 1
        $perFile = 0
    )
    $k -lt $splitGroups
    $(
        $k++ 
        $file++ 
        $perFile += $dtotalPerFile
    ) 
) {
    # stuff
}

Still kind of a mess though.

Working proof of concept (since we don't have your variables):

for (
    $(
        $a = 0
        $b = 10
    )
    $a -lt 10
    $(
        $a++
        $b += 10
    )
) {
    "$a~$b"
}
Sign up to request clarification or add additional context in comments.

7 Comments

perfect, just what I needed. Thank you!
Just for other's reference, change <= to -ile. But other than that, worked perfectly.
@beatles1235 got the second one, Mathias already graciously changed the first one for me.
That may get the job done but it sure gets complicated. I think it would have been simpler to define $file and $perfile before the loop, and drop the $file++ and $perfile++ within the scriptblock of the loop rather than nesting subexpressions like that.
@TheMadTechnician yeah I don't know that I'd actually use this, and it's a bit of a hack, but it is the closest analogue to the C# I could come up with.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.