Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An easy way to output an array in a single line

I have an array in a line of $a=@(1,2,3,4,56). When I do Write-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)?

like image 697
Vesper Avatar asked Aug 04 '15 09:08

Vesper


People also ask

How do you print an array on a single line in Python?

Without using loops: * symbol is use to print the list elements in a single line with space. To print all elements in new lines or separated by comma use sep=”\n” or sep=”, ” respectively.

How do you print output of an array?

We cannot print array elements directly in Java, you need to use Arrays. toString() or Arrays. deepToString() to print array elements. Use toString() method if you want to print a one-dimensional array and use deepToString() method if you want to print a two-dimensional or 3-dimensional array etc.

How do I print an array horizontally?

Set an array. int[] array = new int[] { 50, 100, 150, 200, 250, 300, 350, 400 }; Now, if you will print this array using a loop and new line, then the arrays will be visible vertically. To work it horizontally, use the Join() method and set spaces to separate array elements.

How do you declare and initialize an array in a single line?

We declare an array in Java as we do other variables, by providing a type and name: int[] myArray; To initialize or instantiate an array as we declare it, meaning we assign values as when we create the array, we can use the following shorthand syntax: int[] myArray = {13, 14, 15};


1 Answers

Yes, using -join:

$a -join ','
like image 109
Martin Brandl Avatar answered Sep 24 '22 01:09

Martin Brandl