in reply to Conditionally converting tabs to spaces

Here's a solution...
my $tab = 8; while ( <DATA> ) { chomp; my $fixed = join '', map { $_ . ' 'x($tab-length) } split /\t/; print "$fixed\n"; } __DATA__ hello 57 *45 78 there *57 93 *83 dude 78 23 *45
Enjoy! Update: This code will not work of a field is longer than 8 chars, but the following will:

ps. I don't think any of the other responses take that for account either, but if I'm wrong, I'll retract that.

my $fixed = join '', map { $_ . ' 'x(((int(length()/$tab)+1)*$tab)-len +gth) } split /\t/;
--
Casey

Replies are listed 'Best First'.
RE: Re: Conditionally converting tabs to spaces
by chromatic (Archbishop) on Jul 04, 2000 at 03:19 UTC
    I like this approach, but it's hard not to use sprintf in this case:
    my $tab = 8; while (<DATA>) { chomp; my @fields = split(/\t/, $_); my $cooked = shift (@fields) . join '', sprintf("-$tab%s", @fields +); print $cooked, "\n"; }
    lhoward's looks more elegant, though. Make it use variable tabstops like so:
    my $tab = 8; s/\t/" " x $tab/ego;