Yevgeniy
Yevgeniy

Reputation: 998

Powershell: Format Multidimensional Array for Output

I am exploring options for presenting multidimensional array in some specified format. Desired output is a single line string with elements within each dimension separated by specified character. For example:

$foo = @(@("A","B","C"),@("1","2","3"))
$bar = @()

foreach ($i in $foo)
{
    $bar += $i -Join ", "
}

$bar -join "; "

Produces desired output. When number of dimensions in the array grows, or is variable within the nested elements, this approach becomes cumbersome.

I am wondering if exists some Powershell magic to assist with this task. Ideally, something like:

$foo[([] -join ", ")] -join "; "

And perhaps a solution that will scale well for more complex array configurations.

Thoughts?

Upvotes: 1

Views: 2642

Answers (1)

CB.
CB.

Reputation: 60910

this way?

$foo = @(@("A","B","C"),@("1","2","3"))

($foo | % { $_ -join ',' }) -join '; '

Upvotes: 4

Related Questions