- Ex: 7 Testing whether not writing still copies initial value
- Ex: 6 Working using
[ref]
- Ex: 5 Explicit Get-Variable for all depths
- Ex: 1
- Ex: 2
- Ex: 3 Crazy version
- Ex: 4 Crazy Verbose
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
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
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
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
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
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
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