[tpm] problems with what regex returns ... in context ?

Mike Stok mike at stok.ca
Fri May 10 07:16:01 PDT 2013


My perl is getting a little rusty, but the first thing I thought of was to check the defined-ness of $1 e.g.

my $line        = 'this is stuff in here';
my @patterns    = qw / stuff (stuff) stuff/;
foreach $regex (@patterns) {
    my @capture = ();
    print "regex is '$regex' ... ";
    if ((@capture = ($line =~ m/$regex/)) && defined $1) {
        print "captured " . scalar @capture . " items \n";
        my $i = 0;
        foreach my $item (@capture) {
            print " capture[$i]: '$item'\n";
            $i++;
        }
    } else {
        print "nothing captured\n";
    }
} 

I'm sure there's some problem with that though...

Mike

On 2013-05-10, at 10:06 AM, Fulko Hew <fulko.hew at gmail.com> wrote:

> my $line        = 'this is stuff in here';
> my @patterns    = qw / stuff (stuff) /;
> foreach $regex (@patterns) {
>     my @capture = ();
>     print "regex is '$regex' ... ";
>     if (@capture = ($line =~ m/$regex/)) {
>         print "captured " . scalar @capture . " items \n";
>         my $i = 0;
>         foreach my $item (@capture) {
>             print " capture[$i]: '$item'\n";
>             $i++;
>         }
>     } else {
>         print "nothing captured\n";
>     }
> }

-- 

Mike Stok <mike at stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.




-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/toronto-pm/attachments/20130510/abc794da/attachment.html>


More information about the toronto-pm mailing list