A
A
AlexNov0012021-08-02 14:13:28
PowerShell
AlexNov001, 2021-08-02 14:13:28

From objects to receive the table with a binding to values?

Good day

There are 2 datasets:
1. $objects, consisting of n strings {object}
2. $data_cur, a hash table {name, object, quantity},
filled like this: $data_cur += "" | Select-Object -Property @{N = 'name'; E = {$var1.name}}, @{N = 'object'; E = {$var2.name}, @{N = 'quantity'; E = {$var3.count}}

Data of the form:
name object quantity
name1 object1 1
name1 object5 10
name1 object2 5
name2 object2 1
name2 object3 4

I need to get (as a result) a table of the form:
6107cfe855db2384278275.png
I.e. data mapping between name, object and quantity.
In general, it is enough to get strings like "name1;1;5;0;0;10;0" for csv, etc. so that the values ​​correspond

Please tell me how this can be implemented. Looks simple but doesn't work

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MaxKozlov, 2021-08-03
@AlexNov001

$fields = 'object1','object2','object3','object4','object5','object6'
$data = 'name object quantity
 name1 object1 1
 name1 object5 10
 name1 object2 5
 name2 object2 1
 name2 object3 4' -split "`r`n" | ConvertFrom-Csv -Delimiter ' '

foreach($d in ($data | Group-Object -Property name)) {
    $o = [ordered]@{ name = $d.name }
    $name_data = $d.Group | Group-Object -Property object -AsHashTable
    foreach ($field in $fields) {
        if ($name_data.ContainsKey($field)) {
            $o[$field] = $name_data[$field].quantity
        }
        else {
            $o[$field] = 0
        }
    }
    [PSCustomObject]$o
}

name    : name1
object1 : 1
object2 : 5
object3 : 0
object4 : 0
object5 : 10
object6 : 0

name    : name2
object1 : 0
object2 : 1
object3 : 4
object4 : 0
object5 : 0
object6 : 0

and putting the saza in a pile would be about the same. create $o
in loop fill in fields, convert to
ps object I wonder if powershell highlighting will ever come?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question