Regex to match odd numbers

Dirk Koopman djk at tobit.co.uk
Tue May 27 18:11:05 BST 2014


On 27/05/14 17:21, Abigail wrote:
> On Tue, May 27, 2014 at 04:54:47PM +0100, Dirk Koopman wrote:
>> On 27/05/14 16:22, David Cantrell wrote:
>>> As part of the nasty mess that is CPANdeps, I have this line of code:
>>>
>>> $record->{is_dev_perl} = (
>>>     $record->{perl} =~ /(^5\.(7|9|11|13|15|17|19|21)|rc|patch)/i
>>> ) ? 1 : 0;
>>>
>>> I'd like to not have to remember to add 23 to the list in a year or so's
>>> time. Can anyone think of a nice way of matching any odd number from 7
>>> upwards?  Obviously it's easy to do in a coupla lines of perl code
>>> instead of a regex, so I'm asking more out of curiosity than because I
>>> actually need it.
>>>
>>
>> $bool = ($record->{perl} > 7) & 1;     # for example?
>>
>
>    $ perl -wE 'say +(8 > 7) & 1'
>    1
>    $
>
>
> It would be very odd to consider 8 to be odd.
>

Indeed, hence my follow up.

But this works:

perl -e 'for (0..30) {print ((($_ > 7) && ($_ & 1)) ? "$_ = 1\n" : "$_ = 
0\n")}';

Dirk


More information about the london.pm mailing list