SPUG: more regx stuff

Tim Maher/CONSULTIX tim at consultix-inc.com
Wed Sep 22 21:08:13 CDT 1999


>> Message submitted at: Wed Sep 22 19:08:13 PDT 1999
X-Mailer: ELM [version 2.4 PL25]
MIME-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Content-Length: 2884      

According to Gary Varnell:
> 
> Hello fellow SPUGers
> After last nights meeting I decided to do some regx test.
> I have to admit I had a hard time predicting the results.
> If any one would like the source code that produced the following let me
> know.
> It is pretty basic stuff!

Gary,

Please post the source code so we can see what you did and fool
around with our own modifications.  Incidentally, I'm assuming
that in each of your REs below you meant \S+ when you wrote S+.

Thanks,
Tim

P.S.  More comments below

> 
> Here are two regx test on a list of space seperated names:
tom jones mike smith carl nelson dan strange merylin james
> 
> The first two examples assign the results to an array
> Remeber, since we are assigning to an array 0 = 1 or the first position
> in array
> 
> Here is the # of matches found using /(S+)/g

How did you obtain/print the number of matches?


> ---------------------------------------------
> 9

I got 10, using:  perl -ne '@a=/(\S+)/g; print scalar @a, "\n";' < data_file

> ---------------------------------------------
> 
> Here is the # of matches found using /(S+)/

I got 1.  Once again, how did you obtain/print the number of matches?

> ---------------------------------------------
> 0
> ---------------------------------------------
> 
> Same as above with results assigned to a scaler variable
> 
> Here is the # of matches found using /(S+)/g
> ---------------------------------------------
> 1

That's correct.  In scalar context, the matching operator returns a T/F
code to indicate the success in matching.  If you had looked, you would
have also found the first word in $1, since you used () around the RE.


> ---------------------------------------------
> 
> Here is the # of matches found using /(S+)/
> ---------------------------------------------
> 1

That's also correct.  The /g makes no difference for a single application
of the operator in scalar context.


> ---------------------------------------------
> 
> Supprised?

Nope.  You probably are expecting // to do something different
than it really does, and/or writing the wrong code for your tests.

(Been there, done that, come to grips with it! 8-})

-Tim

> 
> Gary Varnell
> --
> __________________________________________________________
>   Digital Graphics Studios   (DGS)
> ----------------------------------------------------------
>   Website: http://www.2dgs.com      Phone:  (360) 491-1149
>    E-mail: mailto:Info at 2dgs.com
> 
*==================================================================*
| Tim Maher, PhD  CEO, Consultix &    (206) 781-UNIX/8649          |
|      Pacific Software Gurus, Inc.   Email: tim at consultix-inc.com |
| "The UNIX/Perl Training Experts"    http://www.consultix-inc.com |
|Classes: 11/1 Shell/Utils  11/15 Adv Shell  12/6 LINUX  12/13 Perl|
*==================================================================*

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    POST TO: spug-list at pm.org        PROBLEMS: owner-spug-list at pm.org
 Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
 SUBSCRIBE/UNSUBSCRIBE: Replace ACTION below by subscribe or unsubscribe
        Email to majordomo at pm.org: ACTION spug-list your_address





More information about the spug-list mailing list