Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 用于存储字符串的PowerShell数组_String_Powershell_Arrays - Fatal编程技术网

String 用于存储字符串的PowerShell数组

String 用于存储字符串的PowerShell数组,string,powershell,arrays,String,Powershell,Arrays,我有一个文件列表,我正在分解成几个部分。这是可行的,但我希望能够单独引用每个部分。问题在于我的数组不希望/无法接收字符串。文件命名格式为custID\u invID\u prodID或custID\u invID\u prodID\u Boolvalue $files = Get-ChildItem test *.txt [string]$custId = $files.Count [string]$invID = $files.Count [string]$prodID = $files.Co

我有一个文件列表,我正在分解成几个部分。这是可行的,但我希望能够单独引用每个部分。问题在于我的数组不希望/无法接收字符串。文件命名格式为custID\u invID\u prodID或custID\u invID\u prodID\u Boolvalue

$files = Get-ChildItem test *.txt
[string]$custId = $files.Count
[string]$invID = $files.Count
[string]$prodID = $files.Count
[int]$Boolvalue = $files.Count
foreach($file in (Get-ChildItem test *.txt)) {
    for($i = 0; $i -le $files.Count; $i++){
        $custId[$i], $invID[$i], $prodID[$i], $Boolvalue[$i] = $file.BaseName -split "_"
        Write-Host $custId, $invID, $prodID, $Boolvalue
    }
}
我看到的错误消息是:

无法索引到System.String类型的对象


我该怎么做呢?

在我看来,你做得很艰难。为什么一个文件的每个“字段”有4个数组?最好创建数组数组-第一个索引指示文件,第二个索引指示文件中的字段:

$files = Get-ChildItem test *.txt

$arrFiles = @(,@());
foreach($file in $files ) {
    $arrFile = $file.BaseName -split "_"
    $arrFiles += ,$arrFile;
}
Write-Host "listing all parts from file 1:"
foreach ($part in $arrFiles[1]) {
    Write-Host $part
}
Write-Host "listing part 0 from all files":
for ($i=0; $i -lt $arrFiles.Count ; $i++) {
    Write-Host $arrFiles[$i][0];
}

因为您正在尝试索引到System.String类型的对象!您将所有变量设置为字符串,然后尝试分配给索引,我认为这会尝试将字符串分配给您提供的索引处的字符位置

这是未经测试的,但应该是正确的方向

$custIdArr = @()
$invIDArr = @()
$prodIDArr = @()
$BoolvalueArr = @()
foreach($file in (Get-ChildItem test*.txt)) {
    $split = $file.BaseName -split "_"
    $custId = $split[0];  $custIdArr += $custId
    $invID = $split[1];  $invIDArr += $invId
    $prodID = $split[2];  $prodIDArr += $prodID
    $boolValue = $split[3];  $boolValueArr += $boolValue
    Write-Host $custId, $invID, $prodID, $Boolvalue
}
创建一组空数组,在目录中循环,拆分每个文件的文件名,将拆分结果附加到相关数组中


我将分配给
$custId、$invID、$prodID、$Boolvalue
为了上面的清晰起见,您可以选择从
$split
变量直接添加到数组中,即
$invIDArr+=$split[1]

我建议使用对象而不是大量字符串数组。我有一个下面的例子,其中我用一个普通数组替换了文件列表,因为我没有适当的文件结构。只需删除该数组声明并加入
getchilditem
调用,它就可以正常工作了

function ConvertTo-MyTypeOfItem
{
    PARAM (
        [ValidatePattern("([^_]+_){3}[^_]+")]
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [string]$StringToParse
    )

    PROCESS {
        $custId, $invId, $prodId, [int]$value = $StringToParse -split "_"
        $myObject = New-Object PSObject -Property @{
            CustomerID = $custId;
            InvoiceID = $invId;
            ProductID = $prodId;
            Value = $value
        }
        Write-Output $myObject
    }
}

# In the test scenario I have replaced getting the list of files
# with an array of names. Just uncomment the first and second lines 
# following this comment and remove the other $baseNames setter, to
# get the $baseNames from the file listing

#$files = Get-ChildItem test *.txt
#$baseNames = $files.BaseName
$baseNames = @(
    "cust1_inv1_prod1_1";
    "cust2_inv2_prod2_2";
    "cust3_inv3_prod3_3";
    "cust4_inv4_prod4_4";
)

$myObjectArray = $baseNames | ConvertTo-MyTypeOfItem 

$myObjectArray
上述函数将返回具有
CustomerID
InvoiceID
ProductID
属性的对象。在上面的示例中,调用函数并将返回的数组值设置为
$myObjectArray/code>变量。当在控制台中输出时,它将给出以下输出:

InvoiceID CustomerID ProductID Value --------- ---------- --------- ----- inv1 cust1 prod1 1 inv2 cust2 prod2 2 inv3 cust3 prod3 3 inv4 cust4 prod4 4
InvoiceID客户ID产品ID值 --------- ---------- --------- ----- 投资1客户1产品1 投资2客户2产品2 投资3客户3产品3 投资4客户4产品4
我刚刚更新了我的帖子lol。现在列出了文件格式。我把所有的文件按照“u”分成几个部分,并将每个卡盘保存为一个变量,这样我就可以在其他文件中使用它了places@aDroidman好吧,那也没那么好,是吗?:)。你有没有试着估计出需要有多少个文件才能引起这样的问题?我做了,这取决于文件大小。它可以是700个文件或50个。@aDroidman文件大小与它有什么关系?我不是在读取文件内容,而是文件名。。。