Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ninmonkey/fc4f5814cd14f7c304677f4e4658d97f to your computer and use it in GitHub Desktop.
Save ninmonkey/fc4f5814cd14f7c304677f4e4658d97f to your computer and use it in GitHub Desktop.
Join-String is natural for a lot of things, but trying to include the offset is a little unpleasant

Ex: 7 Testing whether not writing still copies initial value

function RenderColsReadOnly {
   param( [string[]]$InText )
   $id = [ref]512   
   $InText | Join-String -sep "`n" -p { 
       # $id.Value++
       Join-String -f "Col_$($id.Value): {0}" -in $_ 
   }
}
RenderColsReadOnly -In ('a'..'b')

outputs

Col_512: b
Col_512: a
function RenderColsWriteBeforeRead { 
   param( [string[]]$InText )
   $id = [ref]512
   $InText | Join-String -sep "`n" -p { 
       $id.Value++
       Join-String -f "Col_$($id.Value): {0}" -in $_
   } 
}
RenderColsWriteBeforeRead -In ('a'..'b')

outputs

Col_513: a
Col_514: b

Ex: 6 Working using [ref]

function RefyRender { 
   param( [string[]]$InText )
   $id = [ref]0  
   $InText | Join-String -sep "`n" -p { 
       $id.Value++
       Join-String -f "Col_$($id.Value): {0}" -in $_
   } 
}
RefyRender -In ('a'..'c')

output

Col_1: a
Col_2: b
Col_3: c

Ex: 5 Explicit Get-Variable for all depths

function DoStuffDebug { 
    $Counter = 100
    'a'..'b'
        | Join-String -sep "`n" -p { 
            
            Join-String -in $_ -p { 

                # when here    
                0..3 + 'local', 'script', 'global' | %{ 
                    $Depth = $_
                    $Value = Get-Variable -scope $Depth -name 'Counter*' -ValueOnly
                    [pscustomobject]@{ 
                        Scope  = $Depth;
                        Value  = [String]::IsNullOrEmpty( $Value ) ? "`u{2400}" : $Value
                        Blank  = [string]::IsNullOrEmpty( $Value )
                        Exists = $Null -eq $Value
                        # Context: 'Inside pipeline | Join-string, then in a non-pipeline join-string'
                    } 
                }
                    | ft |out-string |New-text -fg 'orange' -bg 'gray15' | write-host

            }
        }
}
DoStuffDebug

outputs

 Scope Value Blank Exists
 ----- ----- ----- ------
     0 ␀      True   True
     1 ␀      True   True
     2 100   False  False
     3 ␀      True   True
 local ␀      True   True
script ␀      True   True
global ␀      True   True

Ex: 1

each -Prop iter shadows TopId from the outer JoinStr unless explicit

$Topid = 0
'a'..'d' | Join-String -sep "`n" -p { 
    $script:topId++
    Join-String -f "Col_${script:topid}: {0}" -in $_
} 
Col_1: a
Col_2: b
Col_3: c
Col_4: d

Ex: 2

Ideally I could have it scoped to my function, not script. Or maybe I have to use a ForEach. Then you lose flexibility

function RenderCols { 
   param( [string[]]$InText )
   $script:id = 0  
   $InText | Join-String -sep "`n" -p { 
       $script:id++
       Join-String -f "Col_${script:id}: {0}" -in $_
   } 
}
RenderCols -In ('a'..'e')
Col_1: a
Col_2: b
Col_3: c
Col_4: d
Col_5: e

Ex: 3 Crazy version

to test if value is retrieved once / reset

function RenderCols3 { 
   param( [string[]]$InText )
   $CurOffset = 40
   $InText | Join-String -sep "`n" -p { 
       $curVal  = Get-Variable -scope 1 -Name 'CurOffset' -ValueOnly
       $nextVal = Set-Variable -scope 1 -Name 'CurOffset' -Value ($curVal++)
       Join-String -f "Col_$( Get-Variable -Scope 1 'CurOffSet' -ValueOnly ): {0}" -in $_
   } 
}
RenderCols3 -In ('a'..'e')
Col_40: a
Col_40: b
Col_40: c
Col_40: d
Col_40: e

Ex: 4 Crazy Verbose

function SuperVerboseScopeLogging { 
   param( [string[]]$InText )
   $CurOffset = 40
   $InText | Join-String -sep "`n" -p { 
       "`n## Join-String -Prop iter: `n" | Write-host
       Get-Variable -scope 0 -Name 'CurOffset' -ValueOnly
            | Join-String -op '  => scope 0 := ' | Write-host
       Get-Variable -scope 1 -Name 'CurOffset' -ValueOnly
            | Join-String -op '  => scope 1 := ' | Write-host

       $curVal  = Get-Variable -scope 1 -Name 'CurOffset' -ValueOnly
       $nextVal = Set-Variable -scope 1 -Name 'CurOffset' -Value ($curVal++)

       Get-Variable -scope 0 -Name 'CurOffset' -ValueOnly
            | Join-String -op '  => scope 0 := ' | Write-host
       Get-Variable -scope 1 -Name 'CurOffset' -ValueOnly
            | Join-String -op '  => scope 1 := ' | Write-host

       Join-String -f "Col_$( Get-Variable -Scope 1 'CurOffSet' -ValueOnly ): {0}" -in $_
       "`n" | Write-host
   } 
}
SuperVerboseScopeLogging -In ('a'..'b')
## Join-String -Prop iter:

  => scope 0 :=
  => scope 1 := 40
  => scope 0 :=
  => scope 1 := 40



## Join-String -Prop iter:

  => scope 0 :=
  => scope 1 := 40
  => scope 0 :=
  => scope 1 := 40


Col_40: a
Col_40: b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment