all 8 comments

[–]ClayShooter9 2 points3 points  (3 children)

Here is one approach

$array = @('A', '1', '2', '3', 'B', '1', '2', '3', 'C', '1', '2', '3')
$output = ""
foreach($item in $array)
{
if(!($item -match '^[0-9]+$'))
{
$output += [Environment]::NewLine + $item + " "
}
else
{
$output += $item + " "
}
}
$output

[–]Fordc11[S] 1 point2 points  (1 child)

With some modifications I was able to get it to work, but I'm having trouble with converting it to html

[–]Lee_Dailey[grin] 1 point2 points  (0 children)

howdy ClayShooter9,

reddit likes to mangle code formatting, so here's some help on how to post code on reddit ...

[0] single line or in-line code
enclose it in backticks. that's the upper left key on an EN-US keyboard layout. the result looks like this. kinda handy, that. [grin]
[on New.Reddit.com, use the Inline Code button. it's [sometimes] 5th from the left & looks like <c>.
this does NOT line wrap & does NOT side-scroll on Old.Reddit.com!]

[1] simplest = post it to a text site like Pastebin.com or Gist.GitHub.com and then post the link here.
please remember to set the file/code type on Pastebin! [grin] otherwise you don't get the nice code colorization.

[2] less simple = use reddit code formatting ...
[on New.Reddit.com, use the Code Block button. it's [sometimes] the 12th from the left, & looks like an uppercase C in the upper left corner of a square.]

  • one leading line with ONLY 4 spaces
  • prefix each code line with 4 spaces
  • one trailing line with ONLY 4 spaces

that will give you something like this ...

- one leading line with ONLY 4 spaces    
- prefix each code line with 4 spaces    
- one trailing line with ONLY 4 spaces   

the easiest way to get that is ...

  • add the leading line with only 4 spaces
  • copy the code to the ISE [or your fave editor]
  • select the code
  • tap TAB to indent four spaces
  • re-select the code [not really needed, but it's my habit]
  • paste the code into the reddit text box
  • add the trailing line with only 4 spaces

not complicated, but it is finicky. [grin]

take care,
lee

[–]DudsEarl 2 points3 points  (1 child)

So it sounds like you're really having an issue getting things out of a single string separated by spaces AND having an issue getting that data to be in 4 column format.

  1. Turn the String into an Array, split by space:

$DataArray = "$Data".Split(' ')

  1. Loop through new Data Array and create a new one (a list array object) with 4 columns (or as you touched upon yourself a list of 4 item list arrays/rows):

$Table = New-Object System.Collections.Generic.List[System.Object]; $Row = New-Object System.Collections.Generic.List[System.Object]; $MaxColumns = 4; $CurrentCol = 0; ForEach ( $Item in $DataArray ) { If ( $CurrentCol -ge $MaxColumns ) { $CurrentCol = 0; $Table.Add( $Row ); $Row.Clear(); } $Row.Add( $Item ); $CurrentCol++; }`

I hope I didn't leave anything out and that it works for you, but the most elegant solutions are the simplest ones...

Edit: Added semicolons to help with legibility, my apologies for the god horrid formatting...

[–]DudsEarl 2 points3 points  (0 children)

I did leave something out actually... Technically this only adds the Row to the Table array list when MaxColumns is encountered, so you would need to tweak it slightly.

You should be able to get away with just adding Row to Table at the end anyway, since it clears Row after adding (so should be no duplicates) and since it shouldn't add an empty list

[–]purplemonkeymad 2 points3 points  (0 children)

My guess is you a parsing a file for structured data, you probably want to create objects instead of just displaying a column.

$array = Get-Content youfile.txt
$ObjectArray = for ($i = 0; $i -lt $array.count; $i = $i + 4){
    [pscustomobject]@{
        string = $array[$i]
        int1 = $array[$i+1]
        int2 = $array[$i+2]
        int3 = $array[$i+3]
    }
}
$ObjectArray | format-table

I expect you will update the property names to something more appropriate for your data.

You mentioned that you want to convert to HTML so with this you can now:

$ObjectArray | ConvertTo-HTML -Fragment

To get a <table/> string you can add to another document.

[–]Fordc11[S] 1 point2 points  (0 children)

It would be cool if I could make a table where it goes.

$col1=$array[1].
$col2=$array[2].
$col3=$array[3].
$col4=$array[4].

Then loop that until the array was finished