Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 833007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:27:01+00:00 2026-05-15T04:27:01+00:00

I wanted to detab my source files. (Please, no flame about WHY I wanted

  • 0

I wanted to detab my source files. (Please, no flame about WHY I wanted to detab my sources. That’s not the point 🙂 I couldn’t find a utility to do that. Eclipse didn’t do it for me, so I implemented my own.

I couldn’t fit it into a one liner (-e) program.
I came with the following, which did the job just fine.

while( <> )
{
    while( /\t/ ) {
        s/^(([^\t]{4})*)\t/$1    /;
        s/^((([^\t]{4})*)[^\t]{1})\t/$1   /;
        s/^((([^\t]{4})*)[^\t]{2})\t/$1  /;
        s/^((([^\t]{4})*)[^\t]{3})\t/$1 /;
    }
    print;
}

However, it makes me wonder if Perl – the champion language of processing text – is the right tool. The code doesn’t seem very elegant. If I had to detab source that assume tab=8 spaces, the code would look even worse.

Specifically because I can think of a deterministic state machine with only 4 states to do the job.

I have a feeling that a more elegant solution exists. Am I missing a Perl idiom? In the spirit of TIMTOWTDI I’m curious about the other ways to do it.

u.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T04:27:01+00:00Added an answer on May 15, 2026 at 4:27 am

    I remember a detabify script from one of the O’Reilly books, but I can’t seem to find a link now.

    I have had to solve this problem as well, and I settled on this concise solution to detabify a line:

    1 while $line =~ s/\t/" " x ($tablength - ($-[0] % $tablength))/e ;
    

    In this regular expression $-[0] is the length of the “pre-matched” portion of the line — the number of characters before the tab character.


    As a one-liner:

    perl -pe '1 while s/\t/" "x(4-($-[0]%4))/e' input
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanted to know about Data Type implementation in PHP so I need a
I wanted to know that if I am doing correctly, regarding retain and release
I wanted to try a program distributed in source for *nux on Windows. It's
I wanted some of those spiffy rounded corners for a web project that I'm
just wanted to add this Color Picker ( http://code.google.com/p/devmil-android-color-picker/source/browse/ ) to my app and
I wanted to post this question and answer because I wasn't able to find
I wanted to know if anyone could point me in the right direction for
I wanted to change the default view that loads up in MainWindows.xib. So I
Wanted to add an alias for one of the charsets that PayPal may use
I wanted to comment on a question about interface design and I wanted opinions

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.