Search code examples
arraysstring-conversionapl

APL/APLX - Splitting a string


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.


Solution

  • 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
    ∇