And I suppose a case could be made for not having to
repeat the character being checked for by using
$c = '\'
if($copyloc[-1] -ne $c) {$copyloc += $c}
I guess I'd stop there and say that is enough refactoring.
-------------------------------------------------
now I go over to the "evil" side by adding ...
-------------------------------------------------
Just to show some alternative PowerShell "one-liner"
fun that pulls it all together back to one line
if($copyloc[-1] -ne ($c = '\')) {$copyloc += $c}
and no, I don't think that last one is "better", but
there is a "one-liner" POV that sometimes comes with
PowerShell scripting.
And, to me, an even worse one-liner to consider would be
$copyloc += &{if ($copyloc[-1] -ne ($c = '\')) {$c} else {''}}
although you do get the signal upfront that something may or
may not get appended to $copyloc.
- Larry
On 5/12/2010 12:07 PM, Larry__Weiss wrote:
> PowerShell does allow this equivalent syntax
>
> if($copyloc[-1] -ne "\") {$copyloc += "\"}
>
> And I'm trying hard to not use expandable strings
> unless I really intend them to be expanded, so using
> non-expandable string single quotes would get you to
>
> if($copyloc[-1] -ne '\') {$copyloc += '\'}
>
> - Larry
>
>
> On 5/12/2010 11:25 AM, Justin Rich wrote:
>> I want to make sure a path i've pulled from a config ends
> > with a \ and i've accomplished this doing this
>>
>> if($copyloc[$copyloc.length-1] -ne "\") {$copyloc += "\"}
>>
>> which works fine, im just curious if there is a better way...
>>