- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 this about page ,有一个代码块(如下)展示了 $ForEach
自动变量,但它也有类似批处理子程序的语法。我找不到关于这段代码如何运行或调用什么语言结构的文档。我相信这是 PowerShell v5 的补充,但阅读发行说明对我也没有帮助。 :tokenLoop foreach ($token in $tokens)
是什么意思代表?
function Get-FunctionPosition {
[CmdletBinding()]
[OutputType('FunctionPosition')]
param(
[Parameter(Position = 0, Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[Alias('PSPath')]
[System.String[]]
$Path
)
process {
try {
$filesToProcess = if ($_ -is [System.IO.FileSystemInfo]) {
$_
}
else {
Get-Item -Path $Path
}
foreach ($item in $filesToProcess) {
if ($item.PSIsContainer -or $item.Extension -notin @('.ps1', '.psm1')) {
continue
}
$tokens = $errors = $null
$ast = [System.Management.Automation.Language.Parser]::ParseFile($item.FullName, ([REF]$tokens), ([REF]$errors))
if ($errors) {
Write-Warning "File '$($item.FullName)' has $($errors.Count) parser errors."
}
:tokenLoop foreach ($token in $tokens) {
if ($token.Kind -ne 'Function') {
continue
}
$position = $token.Extent.StartLineNumber
do {
if (-not $foreach.MoveNext()) {
break tokenLoop
}
$token = $foreach.Current
} until ($token.Kind -in @('Generic', 'Identifier'))
$functionPosition = [pscustomobject]@{
Name = $token.Text
LineNumber = $position
Path = $item.FullName
}
Add-Member -InputObject $functionPosition -TypeName FunctionPosition -PassThru
}
}
}
catch {
throw
}
}
}
最佳答案
在 PowerShell 3.0 版及更高版本中(至少从 2.0 版开始),可以选择标记以下语句类型:
switch
foreach
for
while
do
break
的参数。或
continue
标记语句正文中的语句,并将流控制应用于标签指示的语句。
foreach($Name in 'Alice','Bob','Charlie'){
switch($Name.Length){
{$_ -lt 4} {
# We don't have time for short names, go to the next person
continue
}
default {
Write-Host "$Name! What a beautiful name!"
}
}
Write-Host "Let's process $Name's data!"
}
continue
在
Bob
的情况下, 但由于直接父语句是
switch
,它实际上并不适用于
foreach
陈述。
foreach
循环而不是 switch 语句,我们可以避免这种情况:
:outer_loop
foreach($Name in 'Alice','Bob','Charlie'){
switch($Name.Length){
{$_ -lt 4} {
# We don't have time for short names, go to the next person
continue outer_loop
}
default {
Write-Host "$Name! What a beautiful name!"
}
}
Write-Host "Let's process $Name's data!"
}
continue
语句实际上继续循环而不是切换。
break
一起简要讨论在
while
内
about_Break
help topic 中的声明
关于powershell - about_Foreach 示例 : cmd subroutine syntax in PowerShell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46077660/
我有一个名为 Helpers.pm 的模块: use warnings; use 5.012; package Helpers; use Exporter qw(import); our @EXPOR
所以我用 Fortran 90 编写了一个基本的 Vigenere Cypher,但是当我尝试编译它时,由于我的内部子程序,我遇到了无法分类的语句错误。 我已经在'(' ')' 中列出了每个子程序中使
我正在用 C++ 编写图像处理程序。为此,我将第三方程序(边缘检测器)修改为我在我的程序中使用的静态库。 它接缝了原始边缘检测器依赖于操作系统在执行主要功能后清理内存。不幸的是,我修改了这段原始代码后
我最近将我的 .f90 代码更新为 .f03,我期待看到加速,因为我的旧版本在 do 循环(总共 4000 个)内的每次迭代中涉及许多分配和解除分配(7 个 3D 数组--45x45x45)。使用派生
在 Perl 中,我正在学习如何取消引用“子程序引用”。但我似乎无法使用子程序引用作为哈希“键”。 在以下示例代码中, 我可以创建对子例程 ($subref) 的引用,然后取消引用它以运行子例程 (&
我在 another question 中遇到此错误消息我想知道签名中的 $: 部分是什么意思? Cannot resolve caller index(Str: Str, Any); none of
我在编程语言书籍中看到过“子程序”和“例程”。它们是同一个概念吗? “子”是什么意思? 我想除了以下来自 Programming Language Pragmatics, by Scott: 的例子之
我正在尝试使用一个名为 Math::Counting 的模块: #!/usr/bin/perl use strict; use warnings; use Math::Counting; my $f
愚蠢的事情我无法管理... 在 Perl 中,我使用 RRDTool(rrds 模块)创建了一个通用子例程来创建不同时间段(1 天、1 周...)的图表。我想使用一些子参数来显示可选图例。 使用以下代
#include using namespace std; void show_menu () { cout > input; return input; } int process
这一定很简单。不幸的是,我也是。 “filelist.txt”是输出:dir/b 此代码有效:对于“filelist.txt”中的每一行,该行都按预期回显。 for /F "tokens=*" %%A
为了能够使用 Perl 数据语言的常量 PI,我使用 perl -w 运行以下行: use strict; use warnings; use PDL::Constants qw( PI ); 但是,
我的类(class)出现以下错误:“无法修改 file.do 第 26 行的非左值子例程调用。”我的 file.do 看起来像这样: line 2: use BookController; li
通常,当我编写一组 Fortran 函数时,我会将它们放入 MODULE像这样: !mystuff.f90 MODULE mymodule IMPLICIT NONE CONTAINS SUBRO
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我以前用 Perl 编写了一些实用程序,现在我正在重写它们以提供一些新的/更好的功能。然而,事情似乎比原来的实用程序慢得多,所以我决定用 NYTProf 分析器运行一个。伟大的分析器顺便说一句,仍在试
我正在使用一个大型的,旧的,凌乱的,肿的框架。它经常在子例程调用中深入到100多个层次。 Perl调试器认为应该停止并再次将此事实通知我...一遍又一遍。 Package::Stash::name(/
在 this about page ,有一个代码块(如下)展示了 $ForEach自动变量,但它也有类似批处理子程序的语法。我找不到关于这段代码如何运行或调用什么语言结构的文档。我相信这是 Power
我将如何解决以下错误“未定义的子例程 &main::resetCounters 调用于”?该子例程已经原型(prototype)化,但 Perl 仍然提示。以下代码是我遇到的问题: #!/usr/bi
我已经写了一个实现归并排序算法的 MIPS 子程序(代码在帖子的最后)。它接收指向数组的指针及其大小。排序它,不要返回任何东西。 我一直在调试它,修复了一些错误,并且由于子例程在达到基本情况(大小为
我是一名优秀的程序员,十分优秀!