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 more about tools that assist in writing code-generators for C.
I wanted to read .bak files that are the backup files of my ms
I wanted some of those spiffy rounded corners for a web project that I'm
Just wanted to know that does Visual web Developer 2010 [Free tool] contains MVC
I wanted to know that how much easy is to decompile python byte code.
I wanted to compress a folder on a remote namchine.For that i am using
I wanted to write a program which writes to multiple files simultaneously; thought it
Wanted to know, because side effect of this could be to alert user that
I wanted to query my large MYSQL database of users to find the most
I wanted to know is it possible to check that a device can send

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.