[Tutor] Regex's and "best practice"

Karl Pflästerer sigurd at 12move.de
Sun Nov 23 14:49:59 EST 2003

On 23 Nov 2003, Carl D Cravens <- raven at phoenyx.net wrote:

> On Sat, 22 Nov 2003, Karl [iso-8859-1] Pflästerer wrote:

>> Without a little bit cheating you will have more verbose code with
>> Python.  But that's nothing new nor should it be a hindrance (if the
>> number of lines doesn't differ too much).

> I recognize that... took me awhile to get used to, though.  It's been a
> long time since I've done serious C coding, and I've forgotten just how
> much "magic" and assumption there is in Perl.  When I first started

That magic is one of the strengths of Perl but also a great weakness.
You can write fast code (perhaps directly on the command line) but if
that code happens to live in file and has to be maintained that magic
can cause great headaches.

>> That would mean you had to rewrite the regexps a bit.  Second you could
>> use named groups (a nice thing in Python) instead of the numbers.
>> Third you could write one or two functions which give you the terseness
>> of Perl.

> Terseness in the main code, yes... but overall I'd have to write more code
> to develop functions which don't get used anywhere but here.

Once only code is clearly not the greatest strength of Python.  But if
you used such an idiom more often you put that code in your private lib
and import that where you need it.

A little example where I would never think about using Python.  I have a
log file (from my News server with entries from rejected postings); as I
pull news from more than one server there may be severall entries for
the same message (same M-ID).  To shorten the log I use awk like that:
    awk -F"\t" '{ if (mid[$8]++ == 0) print $0 }'
The magic that associative arrays are created automagically and that the
default value for an entry is 0 makes that task in awk extremly easy and
short.  I think in Perl you would write it nearly the same way.  That's
convenient on the command line but who cares for one or two more lines
in a source file?  Short code doesn't necessarily mean fast code (mostly
it's the opposite).

> I'm not following your code... I need to go read about groups.

They can be very convenient if you have deeper nesting.

> My primary purpose in posting was to be sure that I wasn't missing some
> "obvious"  solution.

You didn't (at least IMO).

Please do *not* send copies of replies to me.
I read the list

More information about the Tutor mailing list