Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # will reuse this
- $maxLength = ($z | % Length | measure -max).maximum
- # array count = length of properties (first line of $z), all 0's
- # 0 will represent a whitespace column
- # 1 will represent a non-whitespace column
- $splitGuesses = ,0*$maxLength
- # iterate over columns and rows, if any char is not empty, change the guesses array value to 1 and break
- # the column at that index of the $splitGuesses array represents if the column is whitespace or not
- for ($column=0; $column -lt $maxLength; $column++) {
- for ($row=0; $row -lt $z.Count; $row++) {
- if($z[$row][$column] -ne " ") {
- $splitGuesses[$column] += 1
- break
- }
- }
- }
- # now we need the indices of the split guesses, but need to start w/ 0
- $splitGuessesIndices= @(
- 0
- # iterate over the guesses and add index
- for($i=0; $i -lt $splitGuesses.count; $i++){
- if(-not $splitGuesses[$i]) {
- $i
- }
- }
- )
- # this also needs the max length $z
- $splitGuessesIndices += $maxLength
- # next we need to check each guess (empty column) to confirm the next guess isn't also empty
- # if the next guess was also empty, then the end of the interval is not the beginning of a new property
- # the beginning of a new property must start with a character
- $splitIndices =
- for($i=0; $i -lt $splitGuessesIndices.count - 1; $i++) {
- $index1 = $splitGuessesIndices[$i]
- $index2 = $splitGuessesIndices[$i+1]
- # get substring the text of the first line/row
- $text = -join $z[0][$index1..$index2]
- # if the first line/row is not empty, then the first index is the beginning of a new property column
- if ($text -notmatch '^\s+$') {
- $index1
- }
- }
- # we need the max length of $z to be at the end of the array of indices at which to split
- $splitIndices += $maxLength
- # get property names from first line/row
- $properties = $z[0].Split() | ? {$_}
- # split up the values now
- for($row=1; $row -lt $z.count; $row++) {
- $tempHash = @{}
- # indexes of indexes!
- for($i=0; $i -lt $splitIndices.Count - 1; $i++) {
- # index into $properties for the key name for hash
- # concat the text from the row and trim for value for hash
- $tempHash.Add($($properties[$i]), (-join $z[$row][$splitIndices[$i]..($splitIndices[$i+1])]).Trim())
- }
- # init output array and/or add the hashtable cast as pscustomobject
- [pscustomobject]$tempHash
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement