[英]Is ForEach really an alias of ForEach-Object (Powershell)
ForEach
is documented as an alias of ForEach-Object
. ForEach
被记录为ForEach-Object
的别名。 When I use Get-Alias ForEach
it tells that it is in alias of ForEach-Object
.当我使用Get-Alias ForEach
它告诉我它是ForEach-Object
别名。
But,但,
ForEach-Object
Accepts parameters such as Begin Process and End, where ForEach
doesn't accept them. ForEach-Object
接受Begin Process 和End 等参数,其中ForEach
不接受它们。
When we call ForEach-Object
without any thing it prompts for parameter Process, and on calling ForEach
it leaves a nested prompt with >>
.当我们在没有任何东西的情况下调用ForEach-Object
,它会提示参数 Process,而在调用ForEach
它会留下一个带有>>
的嵌套提示。
%
and ForEach
behave same, but ForEach-Object
don't. %
和ForEach
行为相同,但ForEach-Object
不一样。
Here my questions are:我的问题是:
Is ForEach
really an alias of ForEach-Object
? ForEach
真的是ForEach-Object
的别名吗?
Which is better, ForEach
or ForEach-Object
? ForEach
或ForEach-Object
哪个更好?
Please share your thoughts.请分享您的想法。
Thank you!谢谢!
There are two distinct underlying constructs :有两种不同的底层结构:
The ForEach-Object
cmdlet ForEach-Object
cmdlet
foreach
, which just so happens to match the name of the distinct foreach
statement (see next point).此 cmdlet 有一个内置别名: foreach
,恰好与不同的foreach
语句的名称相匹配(请参阅下一点)。 The foreach
loop statement (akin to the lesser used for
statement). foreach
循环语句(类似于较少使用的for
语句)。
What foreach
refers to depends on the parsing context - see about_Parsing
: foreach
指的是什么取决于解析上下文- 请参阅about_Parsing
:
In argument mode (in the context of a command ), foreach
is ForEach-Object
's alias.在参数模式下(在命令的上下文中), foreach
是ForEach-Object
的别名。
In expression mode (more strictly: statement mode in this case), foreach
is the foreach
loop statement .在表达式模式下(更严格地说:本例中的语句模式), foreach
是foreach
循环语句。
As a cmdlet , ForEach-Object
operates on pipeline input .作为cmdlet , ForEach-Object
对管道输入进行操作。
$_
variable .使用它以流方式处理来自其他命令的输出,逐个对象,因为这些对象通过自动$_
变量被接收。 As a language statement , the foreach
loop operates on variables and expressions (which may include output collected from commands).作为语言语句, foreach
循环对变量和表达式(可能包括从命令收集的输出)进行操作。
Use it to process already-collected-in-memory results efficiently, via a self-chosen iterator variable (eg, $num
in foreach ($num in 1..3) { ... }
);用它来有效地处理已经收集的功能于存储器的结果,通过自选择的迭代变量(例如, $num
中foreach ($num in 1..3) { ... }
); doing so is noticeably faster than processing via ForEach-Object
.这样做明显比通过ForEach-Object
处理快。
Note that you cannot send outputs from a foreach
statement directly to the pipeline, because PowerShell's grammar doesn't permit it;请注意,您不能将foreach
语句的输出直接发送到管道,因为 PowerShell 的语法不允许这样做; for streaming output to the pipeline, wrap a foreach
statement in & { ... }
.要将输出流式传输到管道,请将foreach
语句包装在& { ... }
。 (By contrast, simple expressions (eg, 1..3
) can be sent directly to the pipeline). (相比之下,简单的表达式(例如, 1..3
)可以直接发送到管道)。
For more information, a performance comparison and a discussion of the tradeoffs (memory use vs. performance), including the .ForEach()
array method , see this answer .有关更多信息、性能比较和权衡讨论(内存使用与性能),包括.ForEach()
数组方法,请参阅此答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.