Monday, 11 April 2011

Assigning multiple values in PowerShell

Some time ago I posted a PS one-liner to display the number in the Fibonacci sequence up to 10000000.  I learnt something new about PowerShell today regarding assigning values to variable so thought I’d use the same one-liner to demonstrate.  Originally I assigned a starting value to two variables separately, like this…

$1 = 0; $2 = 1;

The neat trick I found today was that you can do the same with…

$1,$2=0,1;

…which now makes the one-liner now look like…

$1,$2=0,1;$3=$1+$2;do{$3;$1=$2;$2=$3;$3=$1+$2} while ($3 -lt 10000000)

Makes for slightly less typing (well one less “=” anyway, but they all add up for the lazy sysadmin), but its just continues to prove that PowerShell is so flexible in how it lets you use it.

No comments:

Post a Comment