AggroCrag
AggroCrag

Reputation: 317

Error Populating Cells "Unable to index into an object of type"

I am attempting to calculate some basic statistics on a file, and then move the calculated values into an array so I can export them all together as a CSV. The code works great until I hit the very final lines of the code, at which point it throws an error:

Unable to index into an object of type System.Management.Automation.PSObject. 
At C:\debug.ps1:66 char:6
$FinalTable[ <<<< 0].VZA = $VZA

so it's these lines of code that are tripping the error:

$FinalTable[0].VZA = $VZA
$FinalTable[0].VAZ = $VAZ
$FinalTable[0].STDDEV = $StandardDev |

I checked, and the TempOutput2$i.csv file I've imported as $FinalTable contains all of the fields I am trying to populate. Am I not declaring some of the variables correctly along the way somewhere? Or where am I going wrong here?

Here's my code:

$i = 1

While ($i -le 211) {
    # Set the variable to the filename with the iteration number
    $filename = "c:\zMFM\z550Output\20dSummer\fixed20dSum550Output$i.csv"

    # Check to see if that a file with $filename exists. If not, skip to the
    # next iteration of $i. If so, run the code to collect the statistics
    # for each variable and output them each to a different file
    If (Test-Path $filename) {
        #Calculate the Standard Deviation
        #First get the average of the values in the column
        $STDEVInputFile = Import-Csv $filename

        #Find the average and count for column 'td'
        $STDEVAVG = $STDEVInputFile | Measure-Object td -Average | Select Count, Average
        $DevMath = 0

        # Sum the squares of the differences between the mean and each value in the array
        Foreach ($Y in $STDEVInputFile) {
            $DevMath += [math]::pow(($Y.Average - $STDEVAVG.Average), 2)

            #Divide by the number of samples minus one
            $STDEV = [Math]::sqrt($DevMath / ($STDEVAVG.Count-1))

            $StandardDev = $STDEV
        }

        #Calculate the basic statistics for column 'td' with the MEASURE-OBJECT cmdlet
        $STATS = Import-Csv $Filename |
            Measure-Object td -ave -max -min |
            #Export the statistics as a CSV
            Export-Csv -NoType "c:\zMFM\z550Output\20dSummer\tempstats$i.csv"

        #Store the values that will go into the final table as variables
        $GetColumns = Import-CSV $filename
        $VZA = $GetColumns[0].VZA
        $VAZ = $GetColumns[0].VAZ

        #Append the standard deviation variable to the statistics table and add the value

        Import-Csv "c:\zMFM\z550Output\20dSummer\tempstats$i.csv" |
            #Add these fields (columns) to the TempStats file
            #Then populate them using the $GetColumns variable values 
            Select-Object @{Name = "VZA"; Expression = {$_."VZA"}}, @{Name = "VAZ"; Expression = {$_."VAZ"}}, @{Name = "STDDEV"; Expression = {$_."STDDEV"}}, Count, Average, Maximum, Minimum, Property |
            #Export this as TempOutput2$1.csv
            Export-Csv -NoType "c:\zMFM\z550Output\20dSummer\TempOutput2$i.csv"

        #Import it back in as ANOTHER variable
        $FinalTable = Import-Csv "c:\zMFM\z550Output\20dSummer\TempOutput2$i.csv"

        #Populate the fields with the variable values
        $FinalTable[0].VZA = $VZA
        $FinalTable[0].VAZ = $VAZ
        $FinalTable[0].STDDEV = $StandardDev |
            #Export the $STATS file containing everything you need in the correct folder
            Export-Csv -NoType "c:\zMFM\z550Output\20dSummer\Statistics20dSum550.csv"
    }
    $i++
}

Upvotes: 2

Views: 1280

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200293

If your input file has only one data row your output does not become an array unless you enforce it.

$FinalTable = @(Import-Csv "c:\zMFM\z550Output\20dSummer\TempOutput2$i.csv")

Also, if you just need to populate the (additional) columns with values, why don't you already do that when adding the columns?

Import-Csv "c:\zMFM\z550Output\20dSummer\tempstats$i.csv" |
  Select-Object @{Name="VZA";Expression={$VZA}},
                @{Name="VAZ";Expression={$VAZ}},
                @{Name="STDDEV";Expression={$STDDEV}},
                Count, Average, Maximum, Minimum, Property | ...

Upvotes: 2

Related Questions