Consider the example of having as input a list of numbers, some of which are not known exactly but only known to be inside a given range (that is encoded in a string value).

The following code snippet adds up the given numbers, returning an upper and lower bound for the result:

#!/usr/bin/perl use strict; use warnings; my @ranges = ('15', '28-31', '3-4', '40', '17-19'); my ($total_min, $total_max); foreach my $range (@ranges) { my ($min) = $range =~ /^(\d+)/; my ($max) = $range =~ /(\d+)$/; $total_min += $min; $total_max += $max; } print "total is between $total_min and $total_max\n";

It works fine, but regarding the regex part, the need to

always seriously bothers me in cases like this.

I would much prefer to be able to write the whole body of the of loop in the above example in the form:

$total_min += SELF_CONTAINED_FUNCTIONAL_STATEMENT; $total_max += SELF_CONTAINED_FUNCTIONAL_STATEMENT;

Is there an alternative syntax for string extraction using regexes that would allow this?

It's not about performance or such. It's about my brain receiving a nice dose of dopamin whenever I write a line of concise, functional, self-contained code - and the opposite when I can't.

----
PS: Even better would be the following, but unfortunately it seems that Perl's += operator does not work that way:

($total_min, $total_max) += STATEMENT_RETURNING_A_LIST_OF_TWO_NUMS;

In reply to Is there a more functional regex syntax? by smls

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.