1

To get from "a b" to "a b"

ssr["a         b";"[ ]+";" "]

doesn't seem to work. Thanks!

Thomas Smyth - Treliant
  • 4,993
  • 6
  • 25
  • 36
iwbabn
  • 1,275
  • 4
  • 17
  • 32

2 Answers2

2

You can use the following which treats each repeating space as a pair, then using over, 'replaces' these with a single space.

q)x:"This         is    a    test"

q)(" "sv"  "vs)/[x]

"This is a test"
Thomas Smyth - Treliant
  • 4,993
  • 6
  • 25
  • 36
Connor Gervin
  • 935
  • 5
  • 16
  • Pretty neat! Thanks! However, is it not possible to use regex in Q? I just can't get this to work and I have potentially more complicated rules. Thank you again. – iwbabn Apr 13 '16 at 03:22
  • You may need to import an external library as q only supports basic operations ( *,? and [] ) Have a look at http://q.o.potam.us/?p=pcre & http://thesweeheng.wordpress.com/2008/07/14/regular-expressions-for-q/ – Connor Gervin Apr 13 '16 at 03:50
0

It is possible to do this more efficiently then using vs and sv. Using the adverb each-prior ':

{x where not(and':)null x}"This         is    a    test"
"This is a test"

Alternative you can using ssr with the adverb over / in order to continuously remove blocks of two spaces:

ssr[;"  ";" "]/["This         is    a    test"]
"This is a test"

The example you provided fails due to the limited regex options available, using + in this sequence "[ ]+" is an example of an operation that is not supported. You can read more about regex in q on the kx wiki.

Thomas Smyth - Treliant
  • 4,993
  • 6
  • 25
  • 36