Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!apple!usc!elroy.jpl.nasa.gov!jpl-devvax!lwall From: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Newsgroups: comp.lang.perl Subject: Re: Need detab function Keywords: detab Message-ID: <10487@jpl-devvax.JPL.NASA.GOV> Date: 20 Nov 90 20:59:32 GMT References: <1990Nov19.210123.20558@gtisqr.uucp> Reply-To: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Organization: Jet Propulsion Laboratory, Pasadena, CA Lines: 79 In article <1990Nov19.210123.20558@gtisqr.uucp> stu@gtisqr.uucp (Stu Donaldson) writes: : I need a detab function written in perl. I'm sure there must be : something other than the brute force method of implimenting this. : : Any help would be appreciated... : : How about a one liner? :-) Depends on what you mean by "detab". If you mean get rid of leading tabs: s/^(\t+)/' ' x (length($1) * 8)/e; If you mean get rid of all tabs like expand(1) does, then do this: 0 while s/\t/' ' x (8 - length($`) % 8)/e; If you mean to reduce the indentation of your lines, here's a program called retab: #!/usr/bin/perl 'di'; 'ig00'; # # $Header: retab,v 1.1 90/08/12 00:21:20 lwall Locked $ # # $Log: retab,v $ # Revision 1.1 90/08/12 00:21:20 lwall # Initial revision # $sw = 4; $sw = $1, shift if $ARGV[0] =~ /^-(\d+)/; while (<>) { s#^( +)#"\t" x (length($1) / 8) . " " x (length($1) % 8)#e; s#^(\t*)( *)#' ' x ($sw * length($1)) . ' ' x (length($2) * $sw / 8)#e; s#^( +)#"\t" x (length($1) / 8) . " " x (length($1) % 8)#e; print; } ############################################################################## # These next few lines are legal in both Perl and nroff. .00; # finish .ig 'di \" finish diversion--previous line must be blank .nr nl 0-1 \" fake up transition to first page again .nr % 0 \" start at page 1 '; __END__ ############# From here on it's a standard manual page ############ .TH RETAB 1 "August 11, 1990" .AT 3 .SH NAME retab \- change indentation from every 8 columns to fewer columns .SH SYNOPSIS .B retab [-] [files] .SH DESCRIPTION .I Retab reads its input and shrinks all the initial whitespace down by some factor. By default it changes 8 spaces (one hardware tab) to 4 spaces, but you can give a switch to change it to any number of spaces. .PP Processing happens in three stages on each line. First all initial tabs are converted to the equivalent number of spaces. Then the number of spaces are reduced by the specified amount. Then leading spaces are converted back to tabs 8 spaces at a time. .SH ENVIRONMENT No environment variables are used. .SH FILES None. .SH AUTHOR Larry Wall .SH BUGS It might not pick exactly the same spacing you might pick for half-indented lines when converting from, say, 8 to 3 columns per indent. Larry (No, I'm not one of the bugs...)