![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||