通过PowerShell,我试图通过加密货币交换的公共API从所有货币对中获取最后的代码标记数据。为此,我首先得到所有的市场,然后我想循环这些,但出于某种原因,只有第一货币对正在返回。
有人知道我错过了什么吗?
$bt_baseapi_url = "https://bittrex.com/api/v1.1/"
$getmarkets = $bt_baseapi_url + "public/getmarkets"
$getticker = $bt_baseapi_url + "public/getticker"
$markets = Invoke-RestMethod -Uri $getmarkets
$marketnames = $markets.result
foreach ($marketname in $marketnames.marketname) {
$tickerurl = $getticker + "?market=" + $marketname
$ticker = Invoke-RestMethod -Uri $tickerurl
return $ticker.result.last
}发布于 2017-12-28 04:03:44
正如Ansgar Wiechers在关于这个问题的评论中所指出的,在继续循环的同时,不使用 foreach statement's body中的来返回(输出)一个值;return将从任何封闭的函数或脚本返回。
相反,依赖于PowerShell的隐式输出行为,如这个简单示例所示:
> foreach ($el in 1, 2, 3) { $el }
1
2
3只需引用$el而不将其赋值给变量或管道/将其重定向到其他地方,则会使其值被输出。
如果需要,使用来阻止循环主体中后续语句的执行,同时继续整个循环;使用break退出循环。
与此相反--这可能是混乱的根源--作为管道的一部分,ForEach-Object cmdlet call体内的,而不是foreach语句,规则会发生变化,而实际上只会退出手头的迭代,继续执行下一个输入对象:
> 1, 2, 3 | ForEach-Object { return $_ }
1
2
3return $_也只是$_; return的语法糖--即输出生成语句和控制流语句,简单地使用$_可能就足够了。break continue 不与 ForEach-Object cmdlet一起使用 / ,因为这些语句将查找一个封闭的循环语句(例如foreach,do,with`),如果没有这样的语句,则退出整个脚本。- Unfortunately, there is no direct way to _exit_ a pipeline prematurely, - see [https://github.com/PowerShell/PowerShell/issues/3821](https://github.com/PowerShell/PowerShell/issues/3821); make your voice heard there if you think this should change.
https://stackoverflow.com/questions/47999144
复制相似问题