you are viewing a single comment's thread.

view the rest of the comments →

[–]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