Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Re: Empty output oddity in conjunction with foreach statement

Reply
 
Old 03-28-2007   #1 (permalink)
Jacques Barathon [MS]


 
 

Re: Empty output oddity in conjunction with foreach statement

"Keith Hill [MVP]" <r_keith_hill@mailhot.moc.nospamI.please> wrote in
message news:5476E4F2-833B-4578-8041-D46599C51B02@microsoft.com...
<...>
> Yes that works and as it stands is probably the preferred approach.
> Still it strikes me as grossly unintuitive that:
>
> $results = funccall
> foreach ($result in $results) { ... }
>
> behaves differently than
>
> foreach ($result in funccall) { ... }
>
> when funccall returns no output.


Right. All I can guess is that when funccall is directly called from within
the foreach statement it is considered as necessarily being an array,
whereas in the first situation PowerShell cannot determine the type you
expect before you use it in your foreach. If that makes sense...

Jacques


My System SpecsSystem Spec
Old 03-28-2007   #2 (permalink)
Keith Hill


 
 

Re: Empty output oddity in conjunction with foreach statement

"Jacques Barathon [MS]" <jbaratho@online.microsoft.com> wrote in message
news:e2kLbaPcHHA.208@TK2MSFTNGP05.phx.gbl...
> "Keith Hill [MVP]" <r_keith_hill@mailhot.moc.nospamI.please> wrote in
> message news:5476E4F2-833B-4578-8041-D46599C51B02@microsoft.com...
> <...>
>> Yes that works and as it stands is probably the preferred approach.
>> Still it strikes me as grossly unintuitive that:
>>
>> $results = funccall
>> foreach ($result in $results) { ... }
>>
>> behaves differently than
>>
>> foreach ($result in funccall) { ... }
>>
>> when funccall returns no output.

>
> Right. All I can guess is that when funccall is directly called from
> within the foreach statement it is considered as necessarily being an
> array, whereas in the first situation PowerShell cannot determine the type
> you expect before you use it in your foreach. If that makes sense...


Yeah when you look at the individual scenarios it makes sense.
Unfortunately when you use those two scenarios together it doesn't make as
much sense. :-(

To be fair to PowerShell I think this is somewhat in the nature of dynamic
languages, well at least those which do a lot for you under the covers.
Occasionally all that work that the language does for you can actually get
in the way. That means that the programmer using that language has to have
a pretty good grasp on what goes on under the covers lest he or she get bit
by things like this.

--
Keith

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Re: Oddity with ForEach-Object or is it me? PowerShell
break from foreach statement PowerShell
Re: Empty output oddity in conjunction with foreach statement PowerShell
Sorting output from foreach PowerShell
User Guide p.109 - Incorrect statement about foreach PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46