Re: PERL - changing tabs to spaces

by "Kevin Haidl" <kevinh(at)actofmind.com>

 Date:  Thu, 17 Feb 2000 12:10:00 -0800
 To:  <hwg-languages(at)hwg.org>,
"David Moninger" <David.Moninger(at)IslandTime.com>
 References:  0
  todo: View Thread, Original
Part of your confusion is that you think \s refers only to spaces, " ".  In
fact \s is a stand in for any whitespace character, including TAB.  As such,
you can't use it in second division of a tr/// or s/// because Perl doesn't
know which kind of whitespace character you want to insert.  Instead, simply
insert a space in your regex.  As a previous responder noted,

$message =~ s/\t/        /g;

will work.  However,

$message =~ s/\t/ {8}/g;

is a little easier to understand.  Eight spaces are hard to count whereas a
quantifier makes it immediately clear.

Kevin Haidl
Act of Mind Communications
Web Assessment, Site Design, Site Production and Site Promotion
kevinh(at)actofmind.com     http://www.actofmind.com     (604) 734-5098


From: "David Moninger" <David.Moninger(at)IslandTime.com>
Sent: February 17, 2000 10:47 AM
> I'm definitely not a Perl wizard, and I'm sure what I'm trying to do is
> incredably simple, but I've run of things to try!
>
> I have a variable $message which contains a very long text string, with
> many tabs, each occurance of which I would like to replace with 8 spaces.
>
> Would someone please share the proper code to replace a TAB (/t) with 8
> spaces (/s/s/s/s/s/s/s/s) when found in a variable called $message.
>
> I've tried
> use Text::Tabs;
> $tabstop = 8;
>    with expand ... that didn't seem to work,
>
> & I tried
> tr/\t/\s\s\s\s\s\s\s\s/;
> (after putting $message into $_ )
>   but that didn't seem to work.

HWG: hwg-languages mailing list archives, maintained by Webmasters @ IWA