2

私は独学の初心者プログラマーであり、これは非常に基本的な質問であり、コンピューター サイエンスを実際に学んだことがあれば答えられるような質問だと思います :P インタートロンと StackOverflow の検索では、私は持っていません。私が探している答えを見つけることができませんでした。だから誰かが私を甘やかしてくれることを願っています。

私はオブジェクトのコレクションを持っています。一度に 5 つずつ操作してから、次の 5 つに進みたいと思います。コンテキストは、一連の VM を再起動しています。すべての VM が一度に再起動してホストが非難されないように、それらをずらして配置するように求められています。

正しい道はある意味ではfor iループであり、 ではないと感じforeachます。do-untilとの組み合わせのような気がしますfor iが、脳から答えをふるいにかけることはできません。

コレクションからオブジェクトを削除することでおそらくそれを行うことができますが、それがうまくいくとしても、これを行う「間違った」方法のように感じます。

私はこれを Powershell と PowerCLI で行っていますが、私が理解しようとしているロジックは、言語に依存するよりも基本的なものであると感じているので、Powershell に慣れていなくても興味があります。答え。

編集:以下のDavidの回答に基づいて、次のコードが私が探しているもののようです:

$someLetters = @("a","b","c","d","e","f","g","h","i","j","k")

for($i=0; $i -lt $someLetters.length; $i+=5)
{
    Write-Host ("the letter is " + $someLetters[$i] + " and i is " + $i)
    Write-Host ("the letter is " + $someLetters[$i+1] + " and i is " + $i)
    Write-Host ("the letter is " + $someLetters[$i+2] + " and i is " + $i)
    Write-Host ("the letter is " + $someLetters[$i+3] + " and i is " + $i)
    Write-Host ("the letter is " + $someLetters[$i+4] + " and i is " + $i)
    write-host "finished block of five"
}

出力を与えます:

the letter is a and i is 0
the letter is b and i is 0
the letter is c and i is 0
the letter is d and i is 0
the letter is e and i is 0
finished block of five
the letter is f and i is 5
the letter is g and i is 5
the letter is h and i is 5
the letter is i and i is 5
the letter is j and i is 5
finished block of five
the letter is k and i is 10
the letter is  and i is 10
the letter is  and i is 10
the letter is  and i is 10
the letter is  and i is 10
finished block of five

ありがとうデビッド!

4

1 に答える 1

2

オブジェクトがどのようなコンテナに入っているかは言わなかった。配列だと思う。したがって、次のようなことができます。

for($i=0; $i -lt $objectArray.length; $i+=5)
{
    #do something with $objectArray[$i]
}
于 2012-06-13T16:34:50.313 に答える