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

Quantum Mechanic quantum.mechanic.1964 at gmail.com
Fri May 10 10:33:18 PDT 2013


I would have tried "my" instead of the punctuation variables:

if (my @capture = /$regex/) {
    foreach @capture {
        do something;
    }
}

Cheers,
QM

On May 10, 2013, at 4:23 PM, Carlo Costantini <fifteen3 at gmail.com> wrote:

> Because I was following along and thought others might be following as well. I thought I would summarize 
> what you discovered to help you with this situation.
> 
> After looking up $+. I saw that there is also $& which has the string of the last pattern match.
> 
> So to summarize:
> 
> - a successful match with no captured data 
> 
> will have a $& value defined but will not have a $+ value defined.
> 
> - a successful match with captured data 
> 
> will have both $& and $+ defined but you only need to check $+
> 
> - an unsuccessful match
> 
> $& will not be defined
> 
> 
> Thank you for "thinking aloud", Fulko.
> 
> -- 
> Carlo
> 
> 
> On Friday, 10 May, 2013 at 10:23 AM, Fulko Hew wrote:
> 
>> Responding to my own post...
>> 
>> I just discovered that I can test the defined-ness
>> of $+ (the 'last' extraction) because I wasn't sure
>> if $1 $2... would be _also_ defined, if I specified a list to return into.
>> 
>> hmm, yes, $n are populated regardless of the left hand side
>> 
>> Thanks
>> 
>> 
>> On Fri, May 10, 2013 at 10:16 AM, Mike Stok <mike at stok.ca (mailto:mike at stok.ca)> wrote:
>>> 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 (mailto: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 (mailto:mike at stok.ca)>
>>> http://www.stok.ca/~mike/
>>> 
>>> The "`Stok' disclaimers" apply.
>> 
>> _______________________________________________
>> toronto-pm mailing list
>> toronto-pm at pm.org (mailto:toronto-pm at pm.org)
>> http://mail.pm.org/mailman/listinfo/toronto-pm
> 
> 
> 
> _______________________________________________
> toronto-pm mailing list
> toronto-pm at pm.org
> http://mail.pm.org/mailman/listinfo/toronto-pm


More information about the toronto-pm mailing list