>>>>> "BC" == Bob Clancy <[EMAIL PROTECTED]> writes:

  BC> Hello,
  BC>      At our recent Boston.pm social meeting, I mentioned this
  BC>      particular feature of Perl regex's to a few people, and they
  BC>      didn't know about it. I learned about it somewhere in a book.
  BC>      (My recollection is that it is not documented under regex (or
  BC>      regexp?).

  BC> [EMAIL PROTECTED] bclancy]$ showfunc showfunc
  BC> showfunc ()
  BC> {
  BC>      set | perl -ne 'print if m#^'$*' \(\)# .. m#^}$#'

that is the range operator and it is documented in perlop. it has a list
form which generates lists from low to high. the scalar form which you
see above is a logical flip flop (many call it the flipflop
operator). yes, the scalar form is not well known or used a lot. i have
used it several time when parsing out multiple line sections when
reading line by line. but now i slurp in the file and use m//g to grab
all the sections. simpler and faster. but scalar .. has many other uses.

on a side note, don't use # for m// delimiters. i find it very hard to
read and it confuses people with comments. the best alternate delimiter
(recommended by PBP and myself :) is {}. it allows for nested matched
pairs of {} too. but the above regex doesn't have any / in it so it
doesn't need an alternate delimiter. stick with // whenever you
can. using alternate delims is meant for when you have / as data in the
regex.

  BC> }
  BC> [EMAIL PROTECTED] bclancy]$

  BC>      Until I saw this Perl pattern shown above, I used to use awk, and
  BC>      set a variable upon matching the first regex, and clearing a
  BC>      variable upon seeing the second regex, then printing lines when
  BC>      the variable was set.  For example:

  BC> [EMAIL PROTECTED] CTS4.run.2008-06-27T13_58_45,500]$ showfunc awkfunc
  BC> awkfunc ()
  BC> {
  BC>      set | awk 'BEGIN {p=0}; /^'$*' ()/{p=1}; (p); /^}$/{p=0}'

amusing since awk supports the .. concept and perl stole from awk. my
awkfu is extremely corroded but iirc, you just have pair of patterns
separated with , before the block of code. something like this:

/^'$*' ()/, /^}$/ {do something here like print}


that is easier than the perl equiv. i don't use awk at all but as larry
says there have to be some things that awk is better at than perl. :)

uri

-- 
Uri Guttman  ------  [EMAIL PROTECTED]  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------

_______________________________________________
Boston-pm mailing list
[email protected]
http://mail.pm.org/mailman/listinfo/boston-pm

Reply via email to