Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create and populate 2-dimensional array in PowerShell?

Tags:

powershell

I have some data:

id=@(1,2,3)
name=@('Mark','Revekka','Johan')
Height=@(190,'',178)

How can I get one array like table?

1    Mark    190
2    Revekka 
3    Johan   178 

I want to get information like this:

$array[1].name 

and add like this:

$array+=['Helena',168]

Is it possible?

like image 775
Piter Avatar asked Nov 01 '25 02:11

Piter


1 Answers

You could do this:

$data = @(
    [PSCustomObject]@{
        Name = "Mark"
        Height = 190
    },
    [PSCustomObject]@{
        Name = "Revekka"
        Height = ""
    },
    [PSCustomObject]@{
        Name = "Johan"
        Height = 178
    }
)

$data[0].Name # returns Mark

#to add an item you can do

$data += [PSCustomObject]@{
    Name = "Helena"
    Height = 168
}

$data | ft -AutoSize

<# returns

Name    Height
----    ----
Mark     190
Revekka     
Johan    178
Helena   168

#>
like image 103
sodawillow Avatar answered Nov 02 '25 15:11

sodawillow



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!