Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 318
ren Avatar asked Oct 25 '25 23:10

ren


1 Answers

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
∇
like image 88
MBaas Avatar answered Oct 29 '25 19:10

MBaas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!