[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Regex which works, but doesn't



There can be some confusing scoping issues with regex callbacks, and this is
one of them.  The code in question:

> }elsif ( /^\s+VALUE\s*$/ig ) {
> $_ = '     " F3 - CSP415P2 - Salesrep Statistics by Terminal
> -(A) All Accounts".';
> if ( /^\s+\"\s*(S*)F(\d+)\.*(.+)\"/i ) {
> printf "Value 3 find\n";
> }else {
> return;
> }
> }
> if ( defined $1 ) {
> printf " 1: $1\n";
> }
> if ( defined $2 ) {
> printf " 2: $2\n";
> }
> if ( defined $3 ) {
> printf " 3: $3\n";
> }

The essential issue here is that the callbacks ($1, $2, $3) are referring
back to the first regex (/^\s+VALUE\s*$/) and not the second.  To verify
this behavior, use /^(\s+)(VALUE)(\s*)$/ instead on the first regex and you
will see what's happening.  Additionally, put the statement:
    printf "$1 & $2 & $3\n";
after
    printf "Value 3 find\n";

And you will see what I am saying.

On a side note, I recommend against using printf unless you specifically
intend to use it.

Regards,

Keary Suska
Mgr., Information Technologies
Frequent Flyer Services
http://www.webflyer.com/
mailto: aksuska@webflyer.com



# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org