This snippet turns tabs into "jump to the next multiple of four" padding. This is useful for programs in which tab-stops are hardcoded to a fixed location, rather than a fixed length. (Came out of a comment made by davido in the chatterbox.)

Update:

Two strokes of credited to davido ...

Update:

Modification to allow arbitrary tab stop length.

$_="1\t12\t123\t1234\t12345\t"; $s=4;split/\t/;print map{$_.=q/ /x$s;substr $_,0,$s*int(length($_)/$s) +}@_;

Replies are listed 'Best First'.
Re: Tab Stop Converter
by davido (Cardinal) on Sep 29, 2003 at 06:34 UTC
    Here it is as a Swiss Army One Liner.

    First idsfa's golfed to 85 keystrokes:

    perl -pi -e 'split/\t/;map{$_.=q/ /x4;substr$_,0,4*int length($_)/4}@_ +' filename.txt

    And atcroft's golfed to 78 keystrokes:

    perl -pi -e 'while(/\t/g){$d=(4-pos%4)%4+1;$x=q/ /x$d;s/\t/$x/}' filen +ame.txt

    ...both in convenient one line form for your tab-conversion-pleasure. ;)

    Fun exercise. Thanks for the snippets idsfa and atcroft. You guys had working versions while I was still working out the logic on mine (which never saw the light of day).

    Dave

    "If I had my life to do over again, I'd be a plumber." -- Albert Einstein

Re: Tab Stop Converter
by atcroft (Abbot) on Sep 29, 2003 at 06:37 UTC

    Does this code do the same, if the value for $ts is set appropriately?
    perl -p -i.bak -e '$ts=12; while (m/\t/g) { $d= ($ts - (pos % $ts)) % $ts + 1; $x = " " x $d; s/\t/$x/; }' filename.txt

    (I know the -i.bak will create a backup file, but I tend to do that at least until I'm sure I have a replacement working...)

Re: Tab Stop Converter
by jmcnamara (Monsignor) on Sep 29, 2003 at 22:11 UTC
Re: Tab Stop Converter
by bsb (Priest) on Oct 02, 2003 at 08:48 UTC
    Is there code somewhere that will guess the tabstop size?

    I'd like to hook it up to vim so that perl/C/etc sets the tab size as the original author uses
    (modelines obviously won't do)