Workshop
Quiz
1: | What's an efficient way to swap the values contained in two scalar variables $a and $b?
| 2: | 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
| 3: | 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
|
AnswersA1:
| 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. | A2:
| 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. | A3:
| 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
|