Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Is there a more functional regex syntax?

by tobyink (Abbot)
on Sep 18, 2012 at 15:54 UTC ( #994305=note: print w/ replies, xml ) Need Help??


in reply to Is there a more functional regex syntax?

I think this is the prettiest I could make it...

#!/usr/bin/perl use strict; use warnings; my @ranges = ('15', '28-31', '3-4', '40', '17-19'); my @totals = (0, 0); foreach my $range (@ranges) { $totals[$_] += (split /-/, $range)[$_] for 0, -1; } print "total is between $totals[0] and $totals[-1]\n";

... it's arguably less readable though.

perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'


Comment on Re: Is there a more functional regex syntax?
Download Code
Re^2: Is there a more functional regex syntax?
by smls (Friar) on Sep 18, 2012 at 16:42 UTC

    Nice trick using the -1 index like that, I'll have to remember that...

    Regarding the original question though, I was hoping for a solution that keeps the regexes, because even if replacing them with split is possible in this case, that won't always be feasible if dealing with more complex regexes.

      Some regexes will work inside those parentheses.

      $totals[$_] += ($range =~ /(\d+)/g)[$_] for 0, -1;
      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (19)
As of 2014-08-27 19:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (250 votes), past polls