Advertisement
Guest User

Tweaks to Bonus Challenge

a guest
Oct 22nd, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # will reuse this
  2. $maxLength = ($z | % Length | measure -max).maximum
  3.  
  4. # array count = length of properties (first line of $z), all 0's
  5. # 0 will represent a whitespace column
  6. # 1 will represent a non-whitespace column
  7. $splitGuesses = ,0*$maxLength
  8.  
  9. # iterate over columns and rows, if any char is not empty, change the guesses array value to 1 and break
  10. # the column at that index of the $splitGuesses array represents if the column is whitespace or not
  11. for ($column=0; $column -lt $maxLength; $column++) {
  12.     for ($row=0; $row -lt $z.Count; $row++) {
  13.         if($z[$row][$column] -ne " ") {
  14.             $splitGuesses[$column] += 1
  15.             break
  16.         }
  17.     }
  18. }
  19.  
  20. # now we need the indices of the split guesses, but need to start w/ 0
  21. $splitGuessesIndices= @(
  22.   0
  23.   # iterate over the guesses and add index
  24.   for($i=0; $i -lt $splitGuesses.count; $i++){
  25.       if(-not $splitGuesses[$i]) {
  26.           $i
  27.       }
  28.   }
  29. )
  30.  
  31. # this also needs the max length $z
  32. $splitGuessesIndices += $maxLength
  33.  
  34. # next we need to check each guess (empty column) to confirm the next guess isn't also empty
  35. # if the next guess was also empty, then the end of the interval is not the beginning of a new property
  36. # the beginning of a new property must start with a character
  37. $splitIndices =
  38. for($i=0; $i -lt $splitGuessesIndices.count - 1; $i++) {
  39.  
  40.     $index1 = $splitGuessesIndices[$i]
  41.     $index2 = $splitGuessesIndices[$i+1]
  42.  
  43.     # get substring the text of the first line/row
  44.     $text = -join $z[0][$index1..$index2]
  45.  
  46.     # if the first line/row is not empty, then the first index is the beginning of a new property column
  47.     if ($text -notmatch '^\s+$') {
  48.         $index1
  49.     }
  50. }
  51.  
  52. # we need the max length of $z to be at the end of the array of indices at which to split
  53. $splitIndices += $maxLength
  54.  
  55. # get property names from first line/row
  56. $properties = $z[0].Split() | ? {$_}
  57.  
  58. # split up the values now
  59. for($row=1; $row -lt $z.count; $row++) {
  60.     $tempHash = @{}
  61.  
  62.     # indexes of indexes!
  63.     for($i=0; $i -lt $splitIndices.Count - 1; $i++) {
  64.         # index into $properties for the key name for hash
  65.         # concat the text from the row and trim for value for hash
  66.         $tempHash.Add($($properties[$i]), (-join $z[$row][$splitIndices[$i]..($splitIndices[$i+1])]).Trim())
  67.     }
  68.  
  69.     # init output array and/or add the hashtable cast as pscustomobject
  70.     [pscustomobject]$tempHash
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement