4

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?

3
  • It's not really possible. You can have a 2-dimensional array (nested arrays), but they don't have headers so you couldn't do $array[1].name. You could have a 1-d array of objects which would have names, but that isn't a 2-d array. Either way you can't add rows like ['Helena',168] because where's the ID? And it's not clear whether you mean $array[1] to be an array index (element 2) or an ID lookup (element ID==1) - for that to happen it would need to be a dictionary of dictionaries. You can have any of them, but you can't have all of them because they don't make sense together. Commented Jul 12, 2016 at 17:03
  • ok, what's the better decision? Commented Jul 12, 2016 at 17:43
  • I want to have array like in PHP: [ 1=>['name'=>"Mark", 'height'=>190], 2=>['name'=>"Revekka", 'height'=>NULL] 3=>['name'=>"Johan", 'height'=>178] ] Commented Jul 12, 2016 at 17:45

6 Answers 6

9

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

#>
Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

2

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}

Comments

0
$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

1 Comment

remember that arrays start with 0.
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
} 

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.