Detab function

93 views
Skip to first unread message

Dale Worley

unread,
Nov 20, 1990, 1:20:49 PM11/20/90
to

X-Name: Stu Donaldson

I need a detab function written in perl. [...] How about a one liner? :-)

This isn't even ugly:

perl -pe '$spacing = 8; 1 while s/\t/" " x ($spacing - length($`) % $spacing)/e;'

(Amazing! The only other language I know of that can do this in one
line is Snobol!)

However, the apparently equivalent program

perl -pe '$spacing = 8; s/\t/" " x ($spacing - length($`) % $spacing)/eg;'

doesn't work, apparently because s///g doesn't work exactly as I
expect. I expect it to perform each search and replacement, and then
start searching again in the modified string. It appears (if I insert
a strategic print) that the successive searches are done in the
original string, and all modifications are in a copy of it. (Is this
what we really want? Is this documented?)

Dale Worley Compass, Inc. wor...@compass.com
--
It is easier to get forgiveness than permission.

Larry Wall

unread,
Nov 21, 1990, 1:21:08 PM11/21/90
to
In article <1990Nov20.1...@uvaarpa.Virginia.EDU> wor...@compass.uucp writes:
: However, the apparently equivalent program

:
: perl -pe '$spacing = 8; s/\t/" " x ($spacing - length($`) % $spacing)/eg;'
:
: doesn't work, apparently because s///g doesn't work exactly as I
: expect. I expect it to perform each search and replacement, and then
: start searching again in the modified string. It appears (if I insert
: a strategic print) that the successive searches are done in the
: original string, and all modifications are in a copy of it. (Is this
: what we really want? Is this documented?)

I don't know if it's what you want, but it's what you get. :-)

It's documented in The Book, somewhere...

The substitution code is a bit touchy, and if I fiddle with it much
I'll end up rewriting it, which I don't want to do right now.

Larry

Reply all
Reply to author
Forward
0 new messages