Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Re: String values in variables -- SOLVED

Reply
 
Old 10-31-2009   #1 (permalink)
Larry__Weiss


 
 

Re: String values in variables -- SOLVED

Michael Powe wrote:
Quote:

> Okay, I'm an idiot. The problem was in the function call. I forgot
> that you call the function without parens (like calling a sub in VB).
> Once I fixed the function call, I fixed my problem.
>
PowerShell's design of how you can specify a formal parameters, by using
parenthesis around the set of function parameters separated my commas leads us
to expect to carry forward that pattern into the calls. It is just plain
inconsistent.

function subtract($from, $count) { $from - $count }

and the call is

subtract 5 4

Even the alternative of

function subtract { param($from, $count) $from - $count }

uses a pattern involving parenthesis and commas.

And further confusing is that when you invoke .NET methods, you use the more
traditional form of passing arguments. Once you get used to it, it does have
the benefit of distinguishing the PowerShell function and command calls from the
calls to the .NET methods. For example,

subtract "0123456789".IndexOf("5",0) "0123456789".IndexOf("4",0)

(making up a contrived example).

- Larry





My System SpecsSystem Spec
Old 4 Weeks Ago   #2 (permalink)
Thomas Lee


 
 

Re: String values in variables -- SOLVED

In message <ukMTtzkWKHA.2008@newsgroup>, Larry__Weiss
<lfw@newsgroup> writes
Quote:

>PowerShell's design of how you can specify a formal parameters, by
>using parenthesis around the set of function parameters separated my
>commas leads us to expect to carry forward that pattern into the calls.
With the benefit of hindsight, I wished we'd had parens and commas. But
we didn't - and I suspect we'll have to live with his feature.
--
Thomas Lee
doctordns@newsgroup
My System SpecsSystem Spec
Old 4 Weeks Ago   #3 (permalink)
Larry__Weiss


 
 

Re: String values in variables -- SOLVED

Thomas Lee wrote:
Quote:

> In message <ukMTtzkWKHA.2008@newsgroup>, Larry__Weiss
> <lfw@newsgroup> writes
Quote:

>> PowerShell's design of how you can specify a formal parameters, by
>> using parenthesis around the set of function parameters separated my
>> commas leads us to expect to carry forward that pattern into the calls.
>
> With the benefit of hindsight, I wished we'd had parens and commas. But
> we didn't - and I suspect we'll have to live with his feature.
Of course the main reason that commands and parameters are delimited by spaces
alone, is to better follow in the traditions of a command line language, while
allowing PowerShell to have a consistent syntax for both command line and scripting.

In truth, you don't have to use a space as a delimiter. Consider:

PS C:> function add {param($a1,$a2) $a1+$a2}
PS C:> add (1)(2)
3

You just have to "finish" one parameter value and then you can begin the next.
A space character is just the usual way to show a parameter value is completely
specified.

A comma never finishes a parameter value as it just indicates that the value is
to be wrapped as an array. Consider:

PS C:> function addlen {param($a1,$a2) $a1.length+$a2.length}
PS C:> addlen 1 1,2
2
PS C:> addlen 1,2,3 1,2
5
PS C:> addlen (1,2,3)(1,2)
5
PS C:> addlen (1,2,3),(1,2)
2
PS C:> addlen (1,2,3),(1,2) 1,2,3,4,5,6,7,8,9,10
12

- Larry
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Using @ Sign at String Variables? .NET General
Capture elements of a string into variables VB Script
Re: best way to break string into variables PowerShell
Re: gwmi values into variables PowerShell
Shortening string values PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46