简体   繁体   English

Powershell循环引发“数组索引评估为空”

[英]Powershell for loop throwing “the array index evaluated to null”

For starters, I'm on Fedora 30 using PSCore version 6.2.1. 首先,我使用的是PSCore 6.2.1版的Fedora 30。 I've encountered this issue in GNOME Terminal and the vscode snap. 我在GNOME终端和vscode快照中遇到了此问题。

I'm on the first challenge of the PSKoans module and I'm stuck when trying to use a for loop. 我正面临PSKoans模块的第一个挑战,在尝试使用for循环时遇到困难。 I am given an array of strings, each of which is a collection of strings separated by commas. 我得到了一个字符串数组,每个字符串都是用逗号分隔的字符串集合。

$StockData = @(
    "Date,Open,High,Low,Close,Volume,Adj Close"
    "2012-03-30,32.40,32.41,32.04,32.26,31749400,32.26"
    "2012-03-29,32.06,32.19,31.81,32.12,37038500,32.12"
) # The array is much longer than that, but shortened for simplicity's sake

So, my idea is to build a hashtable out of each subsequent string line in the array by using the first string in the array as keys and each following line as a set of values. 因此,我的想法是通过使用数组中的第一个字符串作为键,然后将每一行作为一组值,从数组中的每个后续字符串行构建一个哈希表。 I'm using -split to split the values apart from within the strings. 我正在使用-split将值与字符串内的内容分开。 I want to use a for loop to iterate through the array and pull values, building a hastable in a file to be read later like so: 我想使用一个for循环来遍历数组并提取值,在文件中构建一个hastable以便稍后读取,如下所示:

# Build the array of keys
[array]$keys = $StockData[0] -split ','
# Begin for loop, using $i as int
for ($i = 1, $StockData[$i], $i++) {
    # Create a text file for each hastable
    New-Item -Name "ht$i.txt" -ItemType File
    # Split current string into values
    $values = $StockData[$i] -split ','
    # Set value int
    $valuesInt = 0
    foreach ($key in $keys) {
        Add-Content -Path "./ht$i.txt" -Value "$key = $values[$valuesInt]"
        $valuesInt++
    }
}

As I run that, I get the following error: 运行该命令时,出现以下错误:

Index operation failed; the array index evaluated to null.
At /home/user/PSKoans/Foundations/SolutionStockChallenge.ps1:28 char:6
+ for ($i = 1, $stockData[$i], $i++) {
+      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArrayIndex

I've looked it up and I find all kinds of scenarios in which people get this error message. 我进行了查找,发现各种情况下人们都会收到此错误消息。 I didn't really find a solid explanation for the error message the might lead me to an answer. 我没有真正找到错误消息的可靠解释,这可能会导致我找到答案。

Reading the error message, it doesn't make sense to me. 阅读错误消息,对我来说没有意义。 the array index evaluated to null ...but the array index in the first case is $StockData[1] which is a valid index and should return $true and continue with the loop. the array index evaluated to null ...但在第一种情况下,数组索引为$StockData[1] ,这是有效索引,应返回$ true并继续循环。 Am I missing something? 我想念什么吗?

The syntax of your for loop is wrong. 您的for循环的语法是错误的。 The for loop uses semi-colons as separators. for循环使用分号作为分隔符。

for ($i = 1, $StockData[$i], $i++) {

should be 应该

for ($i = 1; $StockData[$i]; $i++) {

ConvertFrom-Json in PowerShell Core has the coolest switch - AsHashTable. PowerShell Core中的ConvertFrom-Json具有最酷的开关-AsHashTable。 Try this: 尝试这个:

$StockData | convertfrom-csv | convertto-json | ConvertFrom-Json -AsHashtable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM