SORU
13 EKİM 2011, PERŞEMBE


Neden devam dosyalarda grup-Nesne bir mola gibi mi davranıyor?

Eğer powershell komut dosyasında aşağıdaki yaparsam:

$range = 1..100 
ForEach ($_ in $range){
    if ($_ % 7 -ne 0 ) { continue; }
    Write-Host "$($_) is a multiple of 7"
}

Beklenen çıktıyı alıyorum:

7 is a multiple of 7
14 is a multiple of 7
21 is a multiple of 7
28 is a multiple of 7
35 is a multiple of 7
42 is a multiple of 7
49 is a multiple of 7
56 is a multiple of 7
63 is a multiple of 7
70 is a multiple of 7
77 is a multiple of 7
84 is a multiple of 7
91 is a multiple of 7
98 is a multiple of 7

Eğer kullanırsam ancak, bir doğal gaz boru hattı ForEach-Object, devam boru hattı döngü patlak gibi görünüyor.

1..100 | ForEach-Object {
    if ($_ % 7 -ne 0 ) { continue; }
    Write-Host "$($_) is a multiple of 7"
}

Benim soru, ayrılık benim boru hattı yok o yüzden hala yapıyor dosyalarda grup-itiraz ederken, davranış gibi devam edebilir miyim?

CEVAP
14 EKİM 2011, Cuma


Sadece continue yerine return kullanın. Belirli bir yineleme üzerinde ForEach-Object tarafından çağrılan komut bloğu return Bu verir, böylece bir döngü içinde continue bunu taklit eder.

1..100 | ForEach-Object {
    if ($_ % 7 -ne 0 ) { return }
    Write-Host "$($_) is a multiple of 7"
}

Bu yeniden düzenleme konusunda akılda tutulması için alınması gereken bir nokta. Bazen tek istediği dönüştürmek foreach deyimi bloğu içine bir boru hattı ile ForEach-Object çalıştırın (hatta takma foreach yardımcı olun bu dönüşüm kolay ve hatalar yapmak çok kolay;). continue return ile değiştirilmelidir.

P. S. ne yazık ki, bu kadar kolay break taklit ForEach-Object değildir.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • EminemVEVO

    EminemVEVO

    12 Mayıs 2009
  • Jordie Jordan

    Jordie Jorda

    27 Ocak 2008
  • SalGames2016

    SalGames2016

    17 Kasım 2012