passing array of strings into function
im trying pass array of stings function function not seem able see elements in array. here function , how called:
function printstuff
{
param($v1, $v2, [string[]]$arr)
foreach($s in $arr)
{
echo $s
}
}
$a = @("aa","bb","cc")
printstuff("a","b",$a)
i assumed foreach loop go through elements in array , print them out outputs blank. doing wrong here?
change function this
function printstuff
{
param($v1, $v2, [string[]]$arr)
write-host 'write: $arr'
foreach($s in $arr)
{
write-host $s
}
write-host 'write: $v1'
write-host $v1
write-host 'write: $v2'
write-host $v2
}
now check this
ps c:\> printstuff "a" "b" $a
write: $arr
aa
bb
cc
write: $v1
a
write: $v2
b
ps c:\> printstuff ("a","b",$a)
write: $arr
write: $v1
b aa bb cc
write: $v2
maybe see whats happening...
when call function toss stuff @ (not formal typical programming langs)
so when use () grouping data , getting sent first var in function list...
Windows Server > Windows PowerShell
Comments
Post a Comment