String Tabs

From Erlang Community

(Difference between revisions)
Revision as of 21:51, 3 September 2006 (edit)
Bfulgham (Talk | contribs)

← Previous diff
Revision as of 21:31, 21 November 2006 (edit) (undo)
Kaiserpanda (Talk | contribs)
m
Next diff →
Line 49: Line 49:
[[Category:CookBook]][[Category:StringRecipes]] [[Category:CookBook]][[Category:StringRecipes]]
 +
 +
 +
 +[http://www.casino-games-wiki.com/index.php/online_casino_games online casino games]
 +[http://www.magical-casino.com/strategy.html Online casino strategies.]
 +[http://www.casino-games-wiki.com/index.php/online_casino_games online casino games]
 +[http://www.casino-theory.com/online-casino-royale/online-casino-gamble.html online casino gamble]
 +[http://www.gambling-online-theory.com/casinos/strategy-casinos.html strategy casinos]
 +[http://www.magical-casino.com/online_bonus.html Bonuses in Online Casinos.]
 +[http://www.casino-theory.com/online-casino-bonus/index.html online casino bonus]
 +[http://www.slots-wiki.com/index.php/slots_hints slots hints]
 +[http://www.casino-theory.com/online-casino-royale/online-casino-gamble.html online casino gamble]
 +[http://www.gambling-online-theory.com/casinos/on-line-casinos.html on line casinos]

Revision as of 21:31, 21 November 2006

Problem

You want to convert spaces in a string to tabs, or vice versa.

Solution

You can define this by searching for tabs and expanding them as they're found:

-define(TABSTOPS,4).

expand_tabs(Str) ->
    expand_tabs("", Str).

expand_tabs(Accum, Str) ->
    Tabloc = string:chr(Str, $\t),
    case Tabloc of
        0 -> Accum ++ Str;
        _ -> UpToTab = string:substr(Str, 1, Tabloc - 1),
             AfterTab = string:substr(Str, Tabloc + 1),
             Extra = (Tabloc - 1) rem ?TABSTOPS,
             SpaceCount = case Extra of
                              0 -> ?TABSTOPS;
                              _ -> ?TABSTOPS - Extra
                          end,
             expand_tabs(Accum ++ UpToTab ++
               string:chars($ , SpaceCount), AfterTab)
    end.


This looks like this in action:

1> cookbook:expand_tabs("12345\t678\t9").             
"12345   678 9"
2> cookbook:expand_tabs("this is\ta test.").          
"this is a test."
3> cookbook:expand_tabs("this is \ta test.").
"this is     a test."
4> cookbook:expand_tabs("this is  \ta test.").
"this is     a test."
5> cookbook:expand_tabs("this is   \ta test.").
"this is     a test."
6> cookbook:expand_tabs("this is    \ta test.").
"this is     a test."
7> cookbook:expand_tabs("this is     \ta test.").
"this is         a test."

Python includes an expandtabs() method, but Erlang doesn't. This is probably because the tab/space distinction is very important in Python source code, but not so in Erlang. Still, it's a useful thing to do sometimes. The tricky thing about tab expansion is that a tab doesn't translate directly into a fixed number of spaces; you have to calculate the number of spaces for each tab to reach the next tabstop. Tabstops divide the string into equal partitions of the length specified, and the action of a tab character is to move forward to the next tabstop. For example, if your tabstop is 4 and you have the string "12345\t678\t", the first tab converts to 3 spaces and the 2nd converts to one space, e.g. "12345 678 ".


online casino games Online casino strategies. online casino games online casino gamble strategy casinos Bonuses in Online Casinos. online casino bonus slots hints online casino gamble on line casinos

Erlang/OTP Projects
Personal tools