Home > Software design >  Unexpected string to boolean conversion in array, powershell
Unexpected string to boolean conversion in array, powershell

Time:11-15

I am trying to aggregate some statistics from my broker. I have raw data in CSV format. Here are several strings from the CSV:

"Date";"OperationType";"InstrumentType";"Payment";"Currency"
"30.07.2021 6:00:00";"Dividend";"Stock";"3,42";"USD"
"30.07.2021 6:00:00";"Dividend";"Stock";"0,16";"USD"
"29.07.2021 13:55:15";"BrokerCommission";"Currency";"-7,32";"RUB"
"29.07.2021 13:55:14";"Buy";"Currency";"-14635,5";"RUB"
"29.07.2021 13:55:13";"PayIn";;"14642,82";"RUB"
"29.07.2021 6:00:00";"Dividend";"Stock";"1,93";"USD"
"29.07.2021 6:00:00";"Dividend";"Stock";"1107";"RUB"
"29.07.2021 6:00:00";"TaxDividend";"Stock";"-144";"RUB"
"28.07.2021 12:13:18";"BrokerCommission";"Currency";"-7,34";"RUB"
"28.07.2021 12:13:17";"Buy";"Currency";"-14683";"RUB"
"28.07.2021 12:13:17";"PayIn";;"14690,35";"RUB"
"28.07.2021 12:12:38";"BrokerCommission";"Stock";"-0,1";"USD"
"28.07.2021 12:12:37";"Buy";"Stock";"-196,71";"USD"
"28.07.2021 7:58:17";"BrokerCommission";"Currency";"-3,68";"RUB"
"28.07.2021 7:58:16";"Buy";"Currency";"-7369,75";"RUB"
"28.07.2021 7:58:15";"PayIn";;"7373,44";"RUB"
"28.07.2021 0:35:08";"BrokerCommission";"Stock";"-0,06";"USD"
"28.07.2021 0:35:07";"Buy";"Stock";"-122,23";"USD"
"28.07.2021 0:34:16";"BrokerCommission";"Stock";"-0,14";"USD"
"28.07.2021 0:34:15";"Buy";"Stock";"-278,92";"USD"
"28.07.2021 0:33:18";"BrokerCommission";"Stock";"-0,07";"USD"
"28.07.2021 0:33:17";"Buy";"Stock";"-142,76";"USD"
"28.07.2021 0:32:31";"BrokerCommission";"Stock";"-0,04";"USD"
"28.07.2021 0:32:30";"Buy";"Stock";"-84,31";"USD"

Here is the code I use for it:

#Initiate arrays
$InputArray = @()
$FinalArray = @()
$GroupedArray = @()

#Create array with source data
$InputArray = Import-CSV "path_to_csv_file\file.csv" -Delimiter ";" -Encoding UTF8 | Where-Object { $_.PSObject.Properties.Value -ne '' }

#Convert strings to appopriate data types
foreach ($Object in $InputArray){
    $Object.Date = [datetime]::parse($Object.Date) | Get-Date -Format "yyyy-MM-dd"
    $Object.Payment = [double]($Object.Payment -replace(',','.'))
}#foreach

#Group objects
$GroupedArray = $InputArray | Group-Object -Property "Date","Currency","OperationType" | `
Select-Object   @{Name='Date'           ;Expression={$_.Values[0]}}, `
                @{Name='Currency'       ;Expression={$_.Values[1]}}, `
                @{Name='OperationType'  ;Expression={$_.Values[2]}}, `
                @{Name='Payment'        ;Expression={($_.Group | Measure-Object 'Payment' -Sum).Sum}} | `
                Group-Object Date

foreach ($Object in $GroupedArray){
    $Date = $Object.Name
    foreach ($Instance in $Object.Group){
        if ($Instance.OperationType = "BrokerCommission" -and $Instance.Currency -eq "RUB"){
            $BrokerComissionRUB = $null
            $BrokerComissionRUB = $Instance.Payment
        }#if
        if ($Instance.OperationType = "BrokerCommission" -and $Instance.Currency -eq "USD"){
            $BrokerComissionUSD = $null
            $BrokerComissionUSD = $Instance.Payment    
        }#If
        if ($Instance.OperationType = "Dividend" -and $Instance.Currency -eq "RUB"){
            $DividendRUB = $null
            $DividendRUB = $Instance.Payment
        }#if
        if ($Instance.OperationType = "Dividend" -and $Instance.Currency -eq "USD"){
            $DividendUSD = $null
            $DividendUSD = $Instance.Payment    
        }#If
        if ($Instance.OperationType = "PayIn" -and $Instance.Currency -eq "RUB"){
            $PayInRUB = $null
            $PayInRUB = $Instance.Payment
        }#if
    }#foreach

    $FinalArray  = [PSCustomObject]@{
        "Date" = $Date
        "PayInRUB" = $PayInRUB
        "DividendRUB" = $DividendRUB
        "DividendUSD" = $DividendUSD
        "BrokerComissionRUB" = $BrokerComissionRUB
        "BrokerComissionUSD" = $BrokerComissionUSD
    }
}#foreach

$FinalArray

The problem is that in $InputArray value of, for example $InputArrary[1].OperationType, has STRING format:

$InputArray[1].OperationType | gm
TypeName: System.String

But right after grouping objects with Group-Object, OperationType value type is changed to Boolean:

$GroupedArray[1].Group.operationtype | gm
TypeName: System.Boolean

So because of this strange type transformation the actual data is lost. Nevertheless if I execute a part of a script before foreach cycle, the data type of OperationType is still STRING in $GroupedArray. If I run whole script, it changes to Boolean. I have no idea why is that haapening and how can I avoid this. Could you please advice?

CodePudding user response:

There's no strange transformation, You just have a typo, you need to use -eq and not =

You are actually set the $Instance.OperationType value to "BrokerCommission" and then replace it's value again with the results of $Instance.Currency -eq "RUB" which is a boolean value

Check it yourself in the console, execute this:

$Instance.OperationType = "BrokerCommission" -and $Instance.Currency -eq "RUB"

Results:

$Instance.OperationType
False
  • Related