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

Re: [MacPerl] [Q] use of $1, $2...



Nobumi Iyanaga wrote:
> 
> $_ = " abcde. egi";
> 
> if (/^(.+\.)([^\.]*)/) {
>         $a = $1;
>         $a =~ s/ //;
>         $b = $2;
> }
> 
> print "a = $a, b = $b\n";
> 
> When I run this script, the result is:
> 
> a = abcde., b =
> 
> But if I comment out the line "$a =~ s/ //;", I get the expected result:
> 
> a =  abcde., b =  egi
> 
In addition to Chris' excellent response:

The reason the $2 part does not contain anything is, if I read your
expression correctly, that there isn't a period at the beginning of the
line.  Thus the "[^\.]" part would return a _false_.  Also, the asterik
after the close bracket doesn't have a period in front of it.  Thus
making the $2 variable contain nothing but blanks.  I may be wrong about
this, but shouldn't the IF statement be:

	if( /^(.*\.)([^\.].*)/ ){

or just:

	if( /^(.*\.)(.*)/ ){

Which would read:

	IF you find a string at the beginning of a line which ends in a period,
return it as $1.  Then if you also find any more characters - return
them as $2.  (Which isn't exactly the same as having the period in both
strings but is a bit clearer to read.  :-)

--------------------------------------------------------------------------------
All e-mail needs to be sent to mark@cheers.jsc.nasa.gov.  If you don't,
it will
probably bounce.  What man does not understand or fears; he ultimately
destroys.
Steve Wright: Black holes are where God divided by zero.

***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch