4

I have a single string (the result from reading in a paragraph of text) that I'd like to split into an array of words. I'd be splitting the string on every space. This seems like it should be simple but I can't seem to find out a way to accomplish this.

It's worth noting that I'm using APLX, so I do have the option of importing a Ruby function, but I'd much rather stick to APL.

1 Answer 1

9

There are many different way to tackle this. Most often I use the following dfn in Dyalog APL:

penclose←{⎕ML←3 ⋄ ⍺←↑,⍵ ⋄ (~⍵∊⍺)⊂,⍵ } ⍝ separator as ⍺ or ⍵[1]

I'm not sure whether APLX has dfns, so a more "traditional" style would be:

∇ R←penclose R;a
   ⍝ Partitioned enclose of text vector with separator in R[1]
   ⍝ ⎕ML←3  ⍝ Dyalog-specific to make ⊂ APL2-compatible...
 a←1↑R ⋄ R←(~R∊a)⊂R
∇
4
  • In your APLX definition, what does the comma operator do? In my experiments with GNU APL, it seems to suffice to do R←(~R∊a)⊂R Oct 15, 2017 at 20:37
  • Thanks for asking! It was intended as a safety measure against the RANK ERROR you'd as a result of calling penclose''or penclose'x' which are not plausible, but more likely as a result of penclose bool/string - but while the dfn was safe, my translation into traditional style did not properly take care of that. I've fixed it now.
    – MBaas
    Oct 16, 2017 at 11:59
  • In Dyalog you can just use ⊆ instead of ⊂, without setting ⎕ML.
    – Mark Reed
    Aug 13, 2022 at 13:15
  • Yep, that's right. It was added in v16 ( aplwiki.com/wiki/Nest ) - and I probably did not want to use such cutting-edge features then ;)
    – MBaas
    Aug 13, 2022 at 18:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.