Impossible to use a capture as index?

%3CLINGO-SUB%20id%3D%22lingo-sub-2074609%22%20slang%3D%22en-US%22%3EImpossible%20to%20use%20a%20capture%20as%20index%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2074609%22%20slang%3D%22en-US%22%3E%3CP%3EI%20want%20to%20add%20a%20version%20to%20an%20Include%20in%20a%20csproj-file.%20(The%20variable%20%24example%20contains%20only%20an%20Include-statement%20from%20a%20csproj-file)%3C%2FP%3E%3CP%3EI%20have%20a%20hashtable%2C%20with%20the%20name%20as%20a%20key%2C%20and%20an%20array%20of%20strings%20containing%20the%20version%20and%20the%20path.%3C%2FP%3E%3CP%3ESo%20far%2C%20it%20seems%20that%20the%20capture%2C%20%241%2C%20is%20nulled%2C%20as%20soon%20as%20I%20try%20to%20use%20it%20to%20index%20the%20hashtable%2C%20%24example_data.%26nbsp%3B%3C%2FP%3E%3CP%3EIt%20can%20be%20indexed%20with%20a%20variable%2C%20%24example_var%2C%20containing%20exactly%20the%20same%20string%20as%20%241.%3C%2FP%3E%3CP%3E%241%20can%20be%20found%20even%20inside%20the%20variable%20exapansion%20%24()%2C%20but%20*not*%20in%20the%20indexing%20of%20the%20expanded%20hashtable.%3C%2FP%3E%3CP%3EIs%20it%20impossible%2C%20or%20am%20I%20doing%20things%20wrong%20here%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24example_data%20%3D%20%40%7BExample%20%3D%20%40(%221.2.3%22%2C%20%22bin%22)%7D%0A%24example%20%3D%20'%3CNONE%20include%3D%22Example%22%3E%3C%2FNONE%3E'%0A%24example_var%20%3D%20%22Example%22%0A%0A%23%20This%20works%0A%24versioned_example%20%3D%20%24example%20-replace%20'Include%3D%22(.*)%22'%2C%20%22Include%3D%60%22%60%241%60%22-Version%24(%24example_data.Get_Item(%22Example%22)%5B0%5D)%22%0AWrite-Host%20%24versioned_example%0A%0A%23%20This%20works%0A%24versioned_example%20%3D%20%24example%20-replace%20'Include%3D%22(.*)%22'%2C%20%22Include%3D%60%22%60%241%60%22-Version%24(%24example_data.Get_Item(%24example_var)%5B0%5D)%22%0AWrite-Host%20%24versioned_example%0A%0A%23%20This%20kind%20of%20works%20-%20at%20least%20we%20see%20that%20%7B%241%7D%20is%20actually%20%22Example%22%0A%24versioned_example%20%3D%20%24example%20-replace%20'Include%3D%22(.*)%22'%2C%20%22Include%3D%60%22%60%241%60%22-Version%24((%7B%241%7D))%22%0AWrite-Host%20%24versioned_example%0A%0A%23%20But%20this%20does%20not%20work%20-%20why%3F%0A%24versioned_example%20%3D%20%24example%20-replace%20'Include%3D%22(.*)%22'%2C%20%22Include%3D%60%22%60%241%60%22-Version%24(%24example_data.Get_Item(%7B%241%7D)%5B0%5D)%22%0AWrite-Host%20%24versioned_example%0A%0A%23%20But%20this%20does%20not%20work%20-%20why%3F%0A%24versioned_example%20%3D%20%24example%20-replace%20'Include%3D%22(.*)%22'%2C%20%22Include%3D%60%22%60%241%60%22-Version%24(%24example_data.Get_Item(%22%7B%241%7D%22)%5B0%5D)%22%0AWrite-Host%20%24versioned_example%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2074609%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EVisual%20Studio%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Occasional Visitor

I want to add a version to an Include in a csproj-file. (The variable $example contains only an Include-statement from a csproj-file)

I have a hashtable, with the name as a key, and an array of strings containing the version and the path.

So far, it seems that the capture, $1, is nulled, as soon as I try to use it to index the hashtable, $example_data. 

It can be indexed with a variable, $example_var, containing exactly the same string as $1.

$1 can be found even inside the variable exapansion $(), but *not* in the indexing of the expanded hashtable.

Is it impossible, or am I doing things wrong here?

 

 

 

$example_data = @{Example = @("1.2.3", "bin")}
$example = '<None Include="Example" />'
$example_var = "Example"

# This works
$versioned_example = $example -replace 'Include="(.*)"', "Include=`"`$1`"-Version$($example_data.Get_Item("Example")[0])"
Write-Host $versioned_example

# This works
$versioned_example = $example -replace 'Include="(.*)"', "Include=`"`$1`"-Version$($example_data.Get_Item($example_var)[0])"
Write-Host $versioned_example

# This kind of works - at least we see that {$1} is actually "Example"
$versioned_example = $example -replace 'Include="(.*)"', "Include=`"`$1`"-Version$(({$1}))"
Write-Host $versioned_example

# But this does not work - why?
$versioned_example = $example -replace 'Include="(.*)"', "Include=`"`$1`"-Version$($example_data.Get_Item({$1})[0])"
Write-Host $versioned_example

# But this does not work - why?
$versioned_example = $example -replace 'Include="(.*)"', "Include=`"`$1`"-Version$($example_data.Get_Item("{$1}")[0])"
Write-Host $versioned_example

 

 

 

 

0 Replies