Jump to content
ilpostino

Formatere ouput fra array i PowerShell

Recommended Posts

Jeg har en ganske stor multidimensjonal array i PowerShell som jeg ønsker å skrive ut i en meny som brukeren skal velge noe fra. Istedenfor at man må scrolle oppover for å se alle valgene kunne jeg tenke meg å få det dumpet ut i en tabell med for eksempel 4 kolonner slik at jeg får komprimert outputen litt. Hvordan skal jeg få til det?
 
Nedenfor er et eksempelscript som viser hvordan jeg har laget det per nå. (det er noen forskjeller men dette er bare for å få frem poenget / problemet).
 
 

$tall = 0
$tall = 0
$psarray = @(
 ('Line' ,'One'),
 ('Line' ,'Two'),
 ('Line' ,'Tree'),
 ('Line' ,'Four'),
 ('Line' ,'Five'),
 ('Line' ,'One'),
 ('Line' ,'Two'),
 ('Line' ,'Tree'),
 ('Line' ,'Four'),
 ('Line' ,'Five'),
 ('Line' ,'One'),
 ('Line' ,'Two'),
 ('Line' ,'Tree'),
 ('Line' ,'Four'),
 ('Line' ,'Five'),
 ('Line' ,'One'),
 ('Line' ,'Two'),
 ('Line' ,'Tree'),
 ('Line' ,'Four'),
 ('Line' ,'Five'), 
 ('Line' ,'One'),
 ('Line' ,'Two'),
 ('Line' ,'Tree'),
 ('Line' ,'Four'),
 ('Line' ,'Five') 
 )

cls
echo "*** Meny ***"
foreach($item in $psarray) {
    echo "$tall $($item[1])"
    $tall++
}
$MenyTall = $psarray.length - 1

echo "`r`n"
do { $Svar = Read-Host -Prompt "Skriv inn svaret ditt fra menyen over" }
until (($Svar -match "^\d+$") -and ($Svar -In 0..$MenyTall) -eq "True")

Write-Host "Du skrev inn noe fra menyen og det var et tall"

Dette gir outputen under:

*** Meny ***
0 One
1 Two
2 Tree
3 Four
4 Five
5 One
6 Two
7 Tree
8 Four
9 Five
10 One
11 Two
12 Tree
13 Four
14 Five
15 One
16 Two
17 Tree
18 Four
19 Five
20 One
21 Two
22 Tree
23 Four
24 Five


mens jeg vil ha noe ala dette

post-1713-0-22871000-1554314178_thumb.png

Edited by ilpostino

Share this post


Link to post

Ikke prøvd meg så mye på powershell før, men hva med noe som dette:

echo "*** Meny ***"
$columnWidth = 15
for ($i = 0; $i -lt $psarray.length; $i = $i + 4) {
    $outs = @{}
    for ($j = 0; $j -lt 4; $j++) {
        $k = $i + $j
        if ($k -lt $psarray.length) {
            $outs[$j] = "$k $($psarray[$k][1])"
        }
    }
    "{0,-$columnWidth}{1,-$columnWidth}{2,-$columnWidth}{3,-$columnWidth}" -f $outs[0], $outs[1], $outs[2], $outs[3]
}
echo "`r`n"

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...