1

I know it is possible to set mulitple variables to the same value on the same line :

$a, $b = "value"

My question is, Is it possible to do the same thing with Arrays and "+=" operators ?

I've tried this :

 $a = @()
 $b = @()
 $a, $b += "test"

But i get the following error :

Au caractère Ligne:1 : 1
+ $a, $b += test
+ ~~~~~~
L'expression d'affectation n'est pas valide. Vous devez placer un élément affectable, comme une variable ou une propriété, à gauche d'un opérateur
d'affectation.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : InvalidLeftHandSide

Thanks for your help !

1 Answer 1

1

Here's one way to do it in the same line:

$a,$b | foreach {$_ += 'test'}

Edit: that actually doesn't work. This does

$a,$b = $a,$b |% {,($_ += 'test')}

Not sure if that's any better than the iex or not.......

7
  • Thanks ! Are you able to echo the array elements after that ? When I try to get the value of $a or $b nothing is returned.
    – xashcorex
    Jan 29, 2014 at 14:21
  • 1
    @xashcorex try "a","b" | foreach { iex "`$$_ += 'test'"} but I really don't like it ;)
    – CB.
    Jan 29, 2014 at 14:35
  • The reason this doesn't work is that the values of $a and $b are in the pipeline, not the variables themselves. Jan 29, 2014 at 14:43
  • @CB - You're right, that doesn't really work. Seems like it should, tho.
    – mjolinor
    Jan 29, 2014 at 14:43
  • @CB It looks a bit "heavy" but it works haha ;) thanks for your help !
    – xashcorex
    Jan 29, 2014 at 14:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.