Thread: Is there any way for a program to find out how many spaces...?

  1. #16
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by iMalc View Post
    You're still way overthinking things here. The example oogabooga posted is still perfectly correct for your situation.

    If text is copied and pasted from another editor then the tabs within that text will take on the tab positions defined within the editor it is going into, exactly as though you had just typed it in this program.

    There is no differing behaviour per line or per tab. They all fill up to the next multiple of N.
    To replace tabs with spaces in a fixed character width editor, and have it look the same, the only thing you need to know is what is the multiple of character positions to which a tab advances to.
    I don't understand why this is not sinking in for you.
    I hate to disagree with everything you just said, but I'm afraid I'm going to have to, seeing as its all basically wrong from what I can see in this case. There is indeed differing behavior per tab in this particular file, and no, the tabs are neither consistent in their number of spaces, nor are they the same as tabs inserted into the editor with the TAB key. I know, because I have tested it, and my experiences don't agree with what you just said. To prove it, I have attached the file we're all talking about here with said tabs, and you can look for yourself if you want to. Line 656 has a tab character that was copied/pasted from an online source into the editor and saved to file, between the "Media type" and "Subtype columns". That tab has a size of 3 space in length, I believe, and tabs later on in the file have varying lengths. Two more I checked were 5 and 4. As noted before, the default setting of gedit on my computer has a tab size of 8 spaces, and inserts tab characters into the file, and I am still using this default setting, so when I insert a tab with the TAB key in my editor, it creates a tab with 8 spaces (I know, because I have checked).

    Note that I am using the gedit editor (version 2.30.3) in Ubuntu 10.04. Perhaps you would have different results if viewing the file in a different editor or different OS, I don't know. I'm just speaking about my own observations.

    EDIT: And I just checked the file again, and found two more tabs with different lengths. One was 7 spaces long, and another one was 6 spaces long. Now chew on that one for a minute or two...
    Attached Files Attached Files
    Last edited by Programmer_P; 01-17-2012 at 08:08 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Programmer_P
    There is indeed differing behavior per tab in this particular file, and no, the tabs are neither consistent in their number of spaces, nor are they the same as tabs inserted into the editor with the TAB key.
    The lack of consistency in the number of spaces is explained by "fill up to the next multiple of N". Suppose your editor is set to display a tab as 8 spaces. If you enter 3 characters then a tab, the tab will appear to be 5 characters in width since 8 - 3 = 5. If you enter 7 characters then a tab, the tab will appear to be 1 character in width since 8 - 7 = 1. It sounds inconsistent, but there's a method in the madness as iMalc described. The difference between a tab character and "tabs inserted into the editor with the TAB key" is that your editor may be configured to replace tabs entered using the TAB key by spaces. Other than that, no, "tabs inserted into the editor with the TAB key" are actual tab characters.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by laserlight View Post
    The lack of consistency in the number of spaces is explained by "fill up to the next multiple of N". Suppose your editor is set to display a tab as 8 spaces. If you enter 3 characters then a tab, the tab will appear to be 5 characters in width since 8 - 3 = 5. If you enter 7 characters then a tab, the tab will appear to be 1 character in width since 8 - 7 = 1. It sounds inconsistent, but there's a method in the madness as iMalc described. The difference between a tab character and "tabs inserted into the editor with the TAB key" is that your editor may be configured to replace tabs entered using the TAB key by spaces. Other than that, no, "tabs inserted into the editor with the TAB key" are actual tab characters.
    Your logic is still faulty either way, since line 656 in the file has 13 characters before the tab character, and we all know 13 + 3 does not equal 8. Also, even if you begin the count starting at the first space in the line, there is only 2 spaces before the tab character, and 2 + 3 still doesn't equal 8...
    And I am certain that the other lines with tabs in them show similiar facts as well.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Programmer_P
    Your logic is still faulty either way, since line 657 in the file has 13 characters before the tab character, and we all know 13 + 3 does not equal 8.
    13 + 3 = 16. 16 is congruent to 0 (modulo 8).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #20
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by laserlight View Post
    13 + 3 = 16. 16 is congruent to 0 (modulo 8).
    Hmm...true, it is a multiple of 8. But its still not 8.
    Ok, well maybe you have a point there. But I still don't get why tabs would be interpreted this way, instead of having a consistent space between character right before tab character and character right after tab character. And knowing this information still wouldn't have helped me write an algorithm to solve the problem, since I wouldn't have any way of knowing which multiple of 8 the current tab character is actually using. (Note that I'm speaking in the past sense, because I have already finished my program I was writing, and managed to solve the problem using the other solution I already mentioned; so the columns in the file are now all lined up nice, and there are no tabs in the new file)
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Programmer_P
    But I still don't get why tabs would be interpreted this way, instead of having a consistent space between character right before tab character and character right after tab character.
    Notice that your data is in tabular format. One use of tabs is to make it convenient for the columns of a table to be aligned, e.g., the first field of the first column could have 10 characters, the second field could have 12 characters, but with a tab to end the fields, the second column could consistently begin at say, 16 characters.

    Quote Originally Posted by Programmer_P
    And knowing this information still wouldn't have helped me write an algorithm to solve the problem, since I wouldn't have any way of knowing which multiple of 8 the current tab character is actually using.
    If you know which character column the tab is on, you can compute the number of spaces to replace the tab with just as I did in post #17.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by laserlight View Post
    If you know which character column the tab is on, you can compute the number of spaces to replace the tab with just as I did in post #17.
    Using what formula?

    Sorry...I'm not very good with math.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Programmer_P
    Using what formula?
    Let the tab width be N.
    Let the character column of the tab be c. (Column 0 is the first column.)
    Then the number of spaces the tab represents = N - (c % N)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #24
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by laserlight View Post
    Let the tab width be N.
    Let the character column of the tab be c. (Column 0 is the first column.)
    Then the number of spaces the tab represents = N - (c % N)
    Hmm...ok, so if tab width is 8, and c is 13: Divide 13 by 8, you get a remainder of 5. Subtract 5 from 8, you get 3, which is exactly the number of spaces it represents.

    Awesome! Thanks.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  10. #25
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Programmer_P View Post
    Hmm...ok, so if tab width is 8, and c is 13: Divide 13 by 8, you get a remainder of 5. Subtract 5 from 8, you get 3, which is exactly the number of spaces it represents.

    Awesome! Thanks.
    *Facepalm*
    At least someone was able to get the idea across eventually.
    I still feel like I was trying to convince a 50 year old that the tooth fairy isn't real - the whole "how can someone not know that by now" feeling.

    Well at least its all sorted now. As long as you learnt to pay more attention to what others are telling you then all is not lost.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 09-02-2011, 04:57 PM
  2. Program does not printf after spaces.
    By team23 in forum C Programming
    Replies: 5
    Last Post: 09-18-2010, 03:46 PM
  3. Replies: 7
    Last Post: 10-03-2009, 10:58 PM
  4. Replies: 4
    Last Post: 12-01-2007, 04:10 PM
  5. Can't find spaces
    By Drakon in forum C++ Programming
    Replies: 14
    Last Post: 04-07-2004, 10:02 AM