Displaying an array in several columns

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 5 years, 7 months ago.

  • Author
    Posts
  • #5564

    by KaiY at 2013-02-18 01:23:12

    Hi,
    I have a larger array of e.g 1000 elements, which I like to display on screen not only in one but in several columns. Just because of clarity.

    $a=@(1..1000)
    $a | format-table

    1
    2
    .
    1000

    I'm searching for code to get results like
    1 2 3
    4 5 6
    . . .

    thanks
    Kai

    by mjolinor at 2013-02-18 04:51:17

    $w = 3
    $a=@(1..1000)
    for ($i=0; $i -lt $a.count; $i += $w)
    { $a[$i..($i+$w-1)] -join "`t"}

    1 2 3
    4 5 6
    7 8 9
    10 11 12

    (This aligns correctly on my console, but not on the web page)

    by John.A.Mello at 2013-02-18 05:35:45

    You can also use Format-wide, for example
    $array = 1..1000
    $array | Format-Wide {$_} -AutoSize -Force
    $array | Format-Wide {$_} -Column 4 -Force

    The big thing to remember is to include the piped in object in a script block and to use the -Force parameter. Not exactly sure why you need to do this but from what I can tell this is because $array is a simple variable and missing some of the view and or property information that Format-Wide expects. For example the following lists the contents of the variable in one column:
    $array | Format-Wide -Column 4

    by KaiY at 2013-02-18 08:30:16

    Thank you for these solutions! Both are for me quite tricky, but very useful

You must be logged in to reply to this topic.