Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Is there a more functional regex syntax?

by Arunbear (Parson)
on Sep 18, 2012 at 16:19 UTC ( #994309=note: print w/ replies, xml ) Need Help??


in reply to Is there a more functional regex syntax?

A functional approach (more for fun though):

#!/usr/bin/perl use strict; use warnings; use List::Util qw(reduce); use Data::Dump 'pp'; my @ranges = ('15', '28-31', '3-4', '40', '17-19'); my ($total_min, $total_max) = @{ reduce { [$a->[0] + $b->[0], $a->[1] + $b->[1]] } map { @$_ == 1 and [($_->[0]) x 2] or $_ } map { [ split /-/ ] } @ranges }; pp ($total_min, $total_max); __DATA__ ('15', '28-31', '3-4', '40', '17-19') | v ([15], [28, 31], [3, 4], [40], [17, 19]) | v ([15, 15], [28, 31], [3, 4], [40, 40], [17, 19]) | v [103, 109]


Comment on Re: Is there a more functional regex syntax?
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://994309]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (9)
As of 2014-04-16 06:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (415 votes), past polls