PowerShell Cookbook

Search

Categories

 

On this page

Archive

Blogroll

Disclaimer
I work for Microsoft.

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

RSS 2.0 | Atom 1.0 | CDF

Send mail to the author(s) E-mail

Total Posts: 216
This Year: 16
This Month: 0
This Week: 0
Comments: 523

Sign In

 Thursday, July 28, 2005
Thursday, July 28, 2005 10:25:25 PM (Pacific Daylight Time, UTC-07:00) ( )

There are times when you might want to do the same thing many times at the command line.  You normally would use a counted for loop:

MSH:19 C:\temp\monad > for($x = 0; $x -lt 5; $x++) { do-something }

But here's a neat little trick to save some typing, if you don't care which iteration of the loop you're in:

MSH:19 C:\temp\monad > 1..5 | foreach { do-something }

This is a bloated and slow way to do a for loop, though, so don't use it in scripts.

The 1..5 expression creates an array of 5 elements, using the numbers 1 through 5.  Then, we pipe it to foreach-object -- which then performs "do-something" for each element in the array.

For more neat things you can do with arrays, type 
   get-help about_Array
at your prompt.

[Edit: Monad has now been renamed to Windows PowerShell. This script or discussion may require slight adjustments before it applies directly to newer builds.]

Sunday, August 07, 2005 12:38:54 PM (Pacific Daylight Time, UTC-07:00)
Could you explain why "This is a bloated and slow way to do a for loop" because the Hankatsu person seems to think that there is no drawback (or fast).
Clifford Caoile
Sunday, August 07, 2005 12:39:58 PM (Pacific Daylight Time, UTC-07:00)
I meant to add the URL for reference:

http://d.hatena.ne.jp/hankatsu/20050729/1122659503

Clifford Caoile
Sunday, August 07, 2005 8:07:01 PM (Pacific Daylight Time, UTC-07:00)
Sorry, Clifford -- I still can't read what Hankatsu wrote. He or she quoted this post, then expanded it a little bit to make a cool download script.

The reason that this is bloated and slow is that the "1..5" expression actually creates an array of 5 numbers, then passes it down the pipeline. Next, the foreach does one thing for each number in the array.

You can see the efficiency differences with time-expression:

MSH:2 C:\Temp >time-expression { 1..10000 | foreach {} }
TotalMilliseconds : 1078.125

MSH:3 C:\Temp >time-expression { foreach ($x in 1..10000) {} }
TotalMilliseconds : 15.625

One important thing to remember about performance optimizations, though, is that they can always change under you. It is possible for us to make the "bloated and slow" version nearly as fast as the second version, but it probably won't reduce its memory consumption.

Also, with all development -- it's best to program to your intention, not to side-effects. On the command-line, take all of the typing short cuts you want. In a script, though, non-intuitive shortcuts often make your script difficult to read and error prone.

Lee
Name
E-mail
Home page

Comment (Some html is allowed: b, blockquote@cite, em, i, strike, strong, sub, super, u)  

Enter the code shown (prevents robots):