Piter
Piter

Reputation: 117

how to create and populate 2-dimensional array in 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?

Upvotes: 4

Views: 17357

Answers (6)

Podbrushkin
Podbrushkin

Reputation: 510

How can I get one array like table?

First, put your columns into an array:

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

Now you can access second column in second row:

$columns[1][1]    # prints 'Revekka'

If you want an actual table:

$lastColumnIndex = $columns.count - 1
$lastRowIndex = $columns[0].count - 1

0..$lastRowIndex | % {
    $currentRow = $_
    0..$lastColumnIndex | % {$columns[$_][$currentRow]} | Join-String -Separator "`t"
} -OutVariable myTsv

It will produce desired output:

1   Mark    190
2   Revekka 
3   Johan   178

You can add a line with column names: $myTsv = "id`tname`theight", $myTsv:

id      name    height
1       Mark    190
2       Revekka
3       Johan   178

Now, since it's a valid Tsv, you can parse it into an array of PSCustomObject's:

$myTsv | ConvertFrom-Csv -Delimiter `t -OutVariable array
...
id name    height
-- ----    ------
1  Mark    190
2  Revekka
3  Johan   178

Now finally you can get information as you've desired:

$array[1].name  # prints Revekka

Upvotes: 0

why use a 2d array when you can use a class? You can access the class properties using Select-Object -Property

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

class cEntity
{
[int] $id
[string] $name
[float] $weight
}

$list=@()

for($i=0;$i -lt $id.count;$i++)
{
    $obj=[cEntity]::new()
    $obj.id=$id[$i]
    $obj.name=$name[$i]
    $obj.height=$height[$i]
    $list+=$obj
}

cls
foreach ($obj in $list)
{ 
    $val= $obj| Select-Object -Property *
    write-host $val
} 

Upvotes: 0

David Morrow
David Morrow

Reputation: 294

$source = @"
public class MyDouble  
{
     public double[,] MyDbl = new double[,]
     {
         {1, 2, 3},
         {4, 5, 6},
         {7, 8, 9}
     };
}

"@

Add-Type -TypeDefinition $source

$classDbl = [MyDouble]::new()
$classDbl.MyDbl[0,2]
3

$classDbl.MyDbl[2,2] 9

Upvotes: 0

sodawillow
sodawillow

Reputation: 13176

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

#>

Upvotes: 9

Martin Brandl
Martin Brandl

Reputation: 59001

Here another example which iterates over each item and create a PSCustomObject:

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

0 .. ($id.Count -1) | ForEach {
    [PsCustomObject]@{Id = $id[$_]; Name = $name[$_]; Height = $Height[$_];}
}

Output:

Id Name    Height
-- ----    ------
 1 Mark       190
 2 Revekka       
 3 Johan      178

Upvotes: 3

TessellatingHeckler
TessellatingHeckler

Reputation: 29048

To be like the PHP version, it's a dictionary of dictionaries:

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

$data = @{}

$id | foreach {
    $data[$_] = @{
        "Name"=$name[$_-1]
        "Height"=$height[$_-1]
    }
}

$data[1].Name

Then to add a new one, you need to know the next available ID, either keeping track of it elsewhere, or calculating it from the dictionary:

$nextID = ($data.Keys | sort | select -Last 1)  + 1
$data[$nextID] = @{Name='Helena'; 'Height'=168}

Upvotes: 2

Related Questions