3

I'm desesperately trying to adapt the following function from PHP to Powershell :

function lgReplace($origString,$repArray)

{
    // Transforms an input string containing terms like %1%, %2% and so on by values in array
    for($i=1;$i<count($repArray)+1;$i++)
    {
        $origString=str_replace("%$i%",$repArray[$i],$origString);
    }
    return $origString;
}

In php, you can call this function like this :

$source="I like %1% and %2% !";
$new=lgReplace($source, array(1=>"drinking beer",2=>"coding")

In other word, the function will look for "%1%" in $source, change it to "drinking beer", then look for "%2%" in $source, replace it by "coding" then return the result, which is "I like drinking beer and coding !".

I tried to adapt this function to powershell, but failed :

function lgReplace($origString,$repArray)
{
    # Transforms an input string containing terms like %1%, %2% and so on by values in array
    for($i=1;$i -lt $repArray.count+1;$i++)
    {
        $origString=$origString -replace "%$i%",$repArray[$i]
    }
    return $origString
}

$source="I like %1% and %2% !"
$terms=@("coding","drinking beer")
$new=lgReplace $source,$terms
$new

$new displays this :

I like %1% and %2% !
coding
drinking beer

I tried several ways to make this work but to no avail... Any help would be greatly appreciated ! Thanks !

4
  • You can do this cls;$repArray = "drinking", "coding";Write-Output ("I like {0} and {1} !" -f $repArray) Commented May 26, 2017 at 7:57
  • 1
    Thanks Martin, helped me a lot !I don't know how to give you the credits you deserve, I'm new to Stackoverflow... Commented May 26, 2017 at 8:13
  • Looks like someone else had the same idea so you can accept their answer. Commented May 26, 2017 at 8:14
  • 1
    You're a gentleman ! Commented May 26, 2017 at 8:16

2 Answers 2

2

try Something like this (en passant j'adore ton pseudo )

$source="I like {0} and {1} !"
$terms=@("coding","drinking beer")
$new=$source -f $terms
$new
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Esperento57 ! Works like a charm, Martin's comment helped me also a lot !
1

I would prefer to use a hashtable for the [Key - Value] mapping.

$replaceMe = 'I like %1%, %2%, %3%, %4% and %5%'

$keyValueMap = @{
  '%1%' = 'Jägermeister'; 
  '%2%' = 'My wife'; 
  '%3%' = 'PowerShell'; 
  '%4%' = 'the moon';
  '%5%' = 'Hashtable performance'
}

$keyValueMap.GetEnumerator() | % {$replaceMe = $replaceMe -replace $_.key, $_.value }
Write-host $replaceMe 

If I want to compare data structures in PowerShell I wouldn't work with arrays.

In .NET arrays are immutable. Every time you add a new item, the system rebuilds the array and appends the new data.

With each new item your array will gets slower and slower.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.