6

I need to create a function. First variable must be an array with an unknown number of parameters plus another variable. My problem is that I don't know how distinguish between them.

I post an example:

function conc-str {
param([string[]]$array,[string]$name)
foreach($item in $array) {
$str+= $item
}
write-host $str
}

conc-str(@("aa","bb","cc"),"dd") 

The result of this function is

aa bb ccdd

but as you can see I loop array elements and concatenate them. I thought to get just

aa bb cc

Where is my mistake?

2
  • The syntax is a little bit different when compared to most other high level languages. Parameters are not separated by a comma and there are no surrounding braces. Commented Mar 1, 2011 at 6:17
  • Thank you stej for your reply. Commented Mar 1, 2011 at 10:41

1 Answer 1

7

The way you call it is:

conc-str @("aa","bb","cc") "dd"

You don't use "," as a parameter seperator in PowerShell. It is just a space. The moment you put a "," it becomes a single parameter.

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

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.