SPUG: range operator

David Dyck david.dyck at fluke.com
Thu Mar 29 04:27:56 PDT 2007


On Thu, 29 Mar 2007 at 00:26 -0700, Florentin <florentin_ at hotmail.com> wrote:

> # Expect to print only a and b --> prints nothing ??
> while( my $x = <>) {
>      if($x=~m/$start/+1 .. $x=~m/$end/-1){
>          print ;
>      }
> }

Florentine,
    Jacinta did a good job of explaining this, but I think it
would be valuable to take a look at the result of the .. operator
(by temporarily storing in in $t, and then test for the lines
you want to avoid (the first and the last)

       if ( my $t = ($x=~m/$start/ .. $x=~m/$end/) ) {
           print "$t: $x" if $t > 1 && $t !~ /E0$/;
       }

Please revisit
   perldoc perlop

       which documents the value returned as ...

      either the empty string for false, or a sequence number
      (beginning with 1) for true.  The sequence number is reset
      for each range encountered.  The final sequence number in a
      range has the string "E0" appended to it, which doesn't
      affect its numeric value, but gives you something to search
      for if you want to exclude the endpoint.  You can exclude
      the beginning point by waiting for the sequence number to be
      greater than 1.

The above code does print just a and b like you were expecting
(along with the sequence number described above).

Don't feel bad, there aren't any examples in perldoc perlop that actually
show using the result of the operator to exclude the first and last
values.



More information about the spug-list mailing list