Welcome, Guest. Please login or register.

Login with username, password and session length
Pages: [1]   Go Down
Print
Author Topic: Edit tip - condensing whitespace  (Read 2064 times)
0 Members and 1 Guest are viewing this topic.
kenbrody
Administrator

Posts: 82


View Profile
« on: August 22, 2007, 01:07:51 PM »

I recently had someone ask me for a solution to this problem:

  • A file is being imported which contains a field which contains multiple spaces between words, which need to be shrunk to a single space.

I suggested an edit:

    1space    { (" " [{!" "!}]) | * }


It turned out that the field was a name field, and was actually in last name, first name format, and it needed a comma after the last name.  Unfortunately, the names themselves may contain spaces, so inserting a comma before the first space is not an option.  (Also, the first name did not always start in the same column, so MID was not an option, either.)

The following will insert a comma before any set of multiple spaces, and condense any whitespace to a single space:

    ln_fn     { {!" "!}@ | ( <, > !"  "! [{!" "!}] ) | * }

I leave it as an exercise for the reader to determine why this edit needs the {!" "!}@ at the beginning.  Bonus points for answering correctly without actually testing the edit without it.   Smiley
Logged

Kenneth Brody
aka "Mr. filePro"  :-)
kenbrody
Administrator

Posts: 82


View Profile
« Reply #1 on: September 02, 2007, 04:23:40 PM »

I leave it as an exercise for the reader to determine why this edit needs the {!" "!}@ at the beginning.
Well, a couple of people contacted me offlist, but I don't recall seeing the correct answer.  So...

The edit replaces any sequence of spaces with a comma followed by a single space.  Without the {!" "!}@, this would include trailing spaces as well.  (In other words, it would put a comma after all of the text, if there were at least two spaces left at the end of the field.

The edit {!" "!}@ matches (and deletes) all of the trailing spaces.  (It turns out that, in this case, the delete part is unnecessary.  I just used the delete syntax out of habit.)  Once this part of the edit (which must come before the "insert the comma" part), there are no more spaces left for it to match the "insert a comma" part, meaning that a comma will not be added after the text in the field.

Remove the {!" "!}@ part, and you will see, for example, that it converts:
    "Brody     Kenneth     "
to
    "Brody, Kenneth,       "
(Note the trailing comma.)
Logged

Kenneth Brody
aka "Mr. filePro"  :-)
Pages: [1]   Go Up
Print
Jump to:  

Valid XHTML 1.0! Powered by SMF 1.1.15 | SMF © 2011, Simple Machines | Massive Blue Theme By Cadosoas Valid CSS!