代码之家  ›  专栏  ›  技术社区  ›  Grady D

用于存储字符串的PowerShell数组

  •  6
  • Grady D  · 技术社区  · 12 年前

    我有一个文件列表,我正在将其分解为多个部分。这是可行的,但我希望能够单独引用每个部分。问题是我的阵列不想/不能接收字符串。文件命名格式为custID_invID_prodID或custID.invID_predID_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类型的对象。

    我该怎么做?

    3 回复  |  直到 10 年前
        1
  •  6
  •   Robert Westerlund    12 年前

    我建议使用对象而不是大量的字符串数组。我有一个例子,其中我用一个普通数组替换了文件列表,因为我没有文件结构。只需删除该数组声明并放入 Get-ChildItem 打个电话,应该可以正常工作。

    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 Value 财产。在上面的示例中,调用函数并将返回的数组值设置为 $myObjectArray/code> variable. When output in the console it will give the following output:

    InvoiceID                CustomerID               ProductID                                  Value
    ---------                ----------               ---------                                  -----
    inv1                     cust1                    prod1                                          1
    inv2                     cust2                    prod2                                          2
    inv3                     cust3                    prod3                                          3
    inv4                     cust4                    prod4                                          4
    
        2
  •  1
  •   AdamL    12 年前

    在我看来,你做得很艰难。为什么文件的每个“字段”有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];
    }
    
        3
  •  1
  •   arco444    12 年前

    因为您试图索引到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]