|Pathologically Eclectic Rubbish Lister|
The Scalar Range Operatorby pbeckingham (Parson)
|on Jul 26, 2004 at 15:18 UTC||Need Help??|
The Scalar Range Operator
I have been playing with the scalar range operator, and it confused me so much that I started experimenting and reading about it, and here is what I wrote up.
Numeric ValuesThis operator, in scalar context, has two forms that act as a bistable or flip-flop. The first form looks like this:
This if statement works as follows: The condition evaluates false until $left evaluates true. Then the $left condition is ignored, and the condition continues to evaluate true until $right evaluates true, at which point the condition evaluates false, and it goes back to check $left. In this way, it flip-flops between waiting for the left side to evaluate true, and then waiting for the right side to evaluate true. Very strange, until you see it operating in a program:
This program prints out the second and third line of the data. A numeric value in the scalar range operator is therefore compared to $..
Regular ExpressionsThis example shows the use of two regular expressions in the scalar range operator:
It prints out lines in the data beginning with the line that first evaluates true (start), until the line that next evaluates true (end). All the lines that are not bracked by start/end pairs are ignored. Note that this data contains two blocks of lines that are between start and end markers, and the lines outside those ranges are ignored.
Numeric and Regular ExpressionsCombining a numeric and a regex in the range operator also works as expected. In this example, the lines from $. == 1 until $_ =~ /end/ are printed.
Exluding MarkersIn order to exclude lines that contain start and end, a further condition is required. The condition is that the result returned by the scalar range operator must be neither 1 (representing $. of 1), nor must it contain E. When the operator encounters the line that evaluates true for the right-hand-side, it's return value is (in the example below) 5E0. This number evaluates to 5, but contains that E, which is the indicator that this line terminates the right-hand-side of the operator. This code prints all the lines between the start and end lines:
To better illustrate that, this program prints out that value, for all lines between start and end:
Markers on Same LineThis example places both the start and end tokens on the same line. From the output, it can be seen that the combined line has a value of 1E0 which satisfies the test as both the first and last line of the desired input.
The Scalar ... OperatorThe other form of the scalar range operator is .... This operator performs as the .. operator does, but lines that meet one criteria are not also evaluated for the other. So a line that contains both start and end is only evaluated once - in this case for the start line, causing this data to be considered as having a start but not an end, meaning that the data is not properly treated in this example.
This form of the scalar range operator is more efficient only if it is known that both conditions can never be true on the same line.
Update: Changed examples to not include so many edge cases. Thanks to hossman.