Workshop
Quiz
What's an efficient way to swap the values contained in two scalar variables $a and $b?
$a=$b;
($a,$b)=($b, $a);
$c=$a; $a=$b; $b=$c;
What does the statement $a=scalar(@array); assign to the variable $a?
The number of elements in @array
The index of the last element of @array
That syntax is not valid
What does the statement $a=@array; assign to the variable $a?
The number of elements in @array
The index of the last element of @array
That syntax is not valid
Answers
b. The first choice clearly will not work; the value contained in $a is destroyed. Choice c answers the question but requires a third variable to hold the data during the swap. Choice b swaps the data correctly, using no extra variables, and is fairly clear code.
a. Using an array in a scalar context returns the number of elements in the array. $#array would have returned the last index of the array. The use of scalar() in this example is unnecessary; having a scalar on the left side of the assignment operator is enough to put @array in a scalar context.
a. Trick question, it's actually the same as #2, except that the context is implied by the assignment instead of being explicitly given with the scalar operator.
Activities
Modify the Hangman game to print the hangman in an upright position.