Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Quantified named captures in 5.10

by webfiend (Vicar)
on Dec 21, 2007 at 00:45 UTC ( #658340=note: print w/replies, xml ) Need Help??


in reply to Quantified named captures in 5.10

I don't think it's the most graceful solution, but I was able to throw this together in a hurry. It does look like the official rule is that you need to explicitly specify each occurrence of digit that you care about. That doesn't really make sense to me either. Which probably means there is another approach we're missing.

use feature qw(:5.10); use strict; use warnings; my $str = 'Digits: 123689'; if ($str =~ m{Digits: \s* (?<digits>\d+)}x) { my @digits = split //, $+{digits}; say "Digits: " , join(', ', @digits); } else { say "No match"; }

Update: This version looks like a slightly better solution for a more general purpose chain, but I still like it better the way you're thinking of.

use feature qw(:5.10); use strict; use warnings; my $str = 'Digits: 123689'; if ($str =~ m{Digits: \s* (\d+)}x) { my $digit_str = $1; my @digits; while ($digit_str =~ m{(?<digit>\d)}gx) { push @digits, $+{digit}; } say "Digits: " , join(', ', @digits); } else { say "No match"; }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2019-07-22 07:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If you were the first to set foot on the Moon, what would be your epigram?






    Results (11 votes). Check out past polls.

    Notices?