An easy way to output an array in a single line
10,371
Yes, using -join
:
$a -join ','
Author by
Vesper
I'm a hobby programmer, I specialize in common mistakes and blind debugging. I live in Moscow, Russia, and I'm pretty happy with it.
Updated on June 04, 2022Comments
-
Vesper almost 2 years
I have an array in a line of
$a=@(1,2,3,4,56)
. When I doWrite-Debug $a
I get an exception in a line of "Array is not a String". If I do$a | out-string | write-debug
I get a list of values in a column. I can write a function that'll write an array in a row (or a string without newlines), but I'd like to know if I'm inventing a bicycle that exists in Powershell, so I can use an existing solution.Is there a built in function or a not too fancy one-liner to display an array in a format similar to
(1,2,3,4,56)
? -
Vesper over 8 yearsHeh. Easy frag. Thanks.
-
Master Azazel over 5 yearsawesome! way cleaner then doing a foreach imo
-
Atorian over 2 yearsIn case you want to output as multiple lines,
$a -join "`n"
does the job.