[Purdue-pm] tweak to program
Mark Senn
mark at purdue.edu
Sat Mar 27 19:48:25 PDT 2010
The program I sent earlier had
(/^(-?\d+)$/) and $begin = $1, $end = $1;
(/^(-?\d+)-(-?\d+)$/) and $begin = $1, $end = $2;
push @r, $begin..$end;
I changed that to
/^(-?\d+)(?:-(-?\d+))?$/;
$begin = $1;
$end = $2 // $1;
push @r, $begin..$end;
because I think that expresses it better, even though the regular
expression is more complex. (I usually don't use the "group but don't
capture"
(?: ... )
because I don't like that syntax and instead capture everything even if
it is not used later.)
The $2 // $1 means use the value of $2 if it is set, otherwise use the
value of $1.
This code is hard to read if one hasn't seen this technique before but
what I'm trying to express maps nicely to this code.
-mark
More information about the Purdue-pm
mailing list