5

I'm definitely not getting something here:

I'm creating a simple function to replicate a string x times. I am having some weird problem with the parameter -- it doesn't seem to be recognizing the second parameter. When I run the function, it returns an empty string. Further, I think it's lumping the 2 parameters into 1. Here's my code:


Function Repeat-String([string]$str, [int]$repeat) {
  $builder = new-object System.Text.StringBuilder
  for ($i = 0; $i -lt $repeat; $i++) {[void]$builder.Append($str)}
  $builder.ToString()
}

First I dot-source it to load it:

. .\RepeatString.ps1

And then I execute it like this:

Repeat-string("x", 7)
I expected a string of 7 x's. I got an empty string.

I went poking around some more, and I changed the "for" loop. I replaced the "-lt $repeat" part with "-lt 5", so that I would get a fixed number of repeats. When I did that, I got the following output (without the quotes):

Repeat-String("x", 7)

"x 7x 7x 7x 7x 7"

It looks as though it is concatenating the $str and $repeat parameters instead of treating them like 2 separate parameters. What am I doing wrong?

4 Answers 4

17

The problem is that you need to convert your code to the following

Repeat-string "x" 7

In PowerShell, any time you put a group of values inside ()'s, you are creating an array. This means in your sample you're actually passing an array to the function as a single parameter.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! I knew I was doing something stupid -- just wasn't sure what.
@JMarsch, everyone gets tripped up by this at least once. It's really annoying :(
7

Here's a better way, just multiply your (any) string by N repeats:

PS > function Repeat-String([string]$str, [int]$repeat) {  $str * $repeat }
PS > Repeat-String x 7
xxxxxxx

PS > Repeat-String JMarsch 3
JMarschJMarschJMarsch

2 Comments

Every new thing I learn about powershell just shows me how cool it is. Thanks for sharing that, Shay +1
In a way, Shay Levy's comment shows that we could just use the "multiply string" feature instead of writing a user-defined-function.
6

A noted Chinese proverb states:

"Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime."

For the cosmopolitan-minded among you, the original is 授人以魚不如授人以漁 and the Pinyin romanization is Shòu rén yǐ yú bùrú shòu rén yǐ yú. (I have been learning to speak a smattering of Mandarin and to write Pinyin so I have to practice! Curiously I found the above Pinyin expression seemed to be an enigma, but that is a story for another time...:-)

Now to my point: JMarsch came upon a common PowerShell pitfall liable to trip up anyone used to "conventional" languages, and @JaredPar provided the correct resolution. I submit, though, that answer is akin to slapping down a fish in front of you!

Just published on Simple-Talk.com, Down the Rabbit Hole: A Study in PowerShell Pipelines, Functions, and Parameters discusses the above pitfall along with many other nuances of the function-calling interface. One section of my article, for example, covers the subtle differences between all of the following calls to function f, most of which will not yield what you expect.

f(1,2,3)
f (1,2,3)
f 1,2,3
f (1 2 3)
f 1 2 3

Look for the PDF download of the handy wallchart reference accompanying the article, too.

Here's a thumbnail: PowerShell Functions Quick Reference

(Oh, and you may want to also check out the list of humorous spin-offs of the above classic quote at Give a man a fish...)

1 Comment

What does the fish have to do with it? :)
0

Of course JaredPar is right.

I like to use the built in range function .. for this too: (notice I start at 1 instead of 0)

Function Repeat-String([string]$str, [int]$repeat) {
  $builder = new-object System.Text.StringBuilder
  1..$repeat | %{ [void]$builder.Append($str) }
  return $builder.ToString()
}

Repeat-string "x" 7

1 Comment

Thanks -- didn't know about the range function. I'm going to have to fit a book on powershell into my reading list. (I guess at this point, I might as well look/wait for a powershell 2.0 book)

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.