Disclaimer
⎕S
is a powerful beast with many capabilities - but I'm not sure it is the right tool for such a simple job as checking if there is a match or not. But maybe you tried to simplify a complex problem - or maybe I'm not understanding ⎕S
well enough.
So I'd like to show how I would solve the given problem and will then look potential ⎕S
-approaches.
Find ⍷
Find returns a boolean vector with ones at the starting position of the searched string.
So I'd a find in each of the vectors and then an ∨/
(or-reduce) to determine if there is a match:
∨/¨'foo'∘⍷¨'foo' 'bar' 'foofoo'
1 0 1
⎕S
With ⎕S, you can search using regex even on a nested argument. So we'll need to use the each-operator ¨
to apply ⎕S f
on each of the 3 vectors.
{('foo'⎕S f)⍵}¨'foo' 'bar' 'foofoo'
Then there are various things it could return:
- the lengths of any matches: using
f
=tranformation-code 1
, it would return the length of the matching strings. Since we have a fixed pattern, it would be any number of 3s (zero to n). In your example:
{('foo'⎕S 1 )⍵}¨'foo' 'bar' 'foofoo'
┌─┬┬───┐
│3││3 3│
└─┴┴───┘
Finally, check if we have any 3s, and we have the desired result:
3∊¨{('foo'⎕S 1 )⍵}¨'foo' 'bar' 'foofoo'
1 0 1
- the matching string itself. Using transformation patter
&
, it would return the matching string:
{('foo'⎕S '&' )⍵}¨'foo' 'bar' 'foofoo'
┌─────┬┬─────────┐
│┌───┐││┌───┬───┐│
││foo││││foo│foo││
│└───┘││└───┴───┘│
└─────┴┴─────────┘
Check if there are matches and we're done:
0<≢¨{('foo'⎕S '&' )⍵}¨'foo' 'bar' 'foofoo'
1 0 1
- and finally, just to cover that one, too - with a transformation function. The TF is called for each match that was found is is passed a namespace with various fields describing the match. (The help documents the various possibilities, I'm going to use
.Lengths
which gives the same a transformation-code 1
in the earlier example)
{('foo'⎕S {⍵.Lengths} )⍵}¨'foo' 'bar' 'foofoo'
┌───┬┬─────┐
│┌─┐││┌─┬─┐│
││3││││3│3││
│└─┘││└─┴─┘│
└───┴┴─────┘
0<≢¨{('foo'⎕S {⍵.Lengths} )⍵}¨'foo' 'bar' 'foofoo'
1 0 1
Actually, we don't need Lengths
at all - we can just check if it gets any calls:
∨/¨{('foo'⎕S {0<≢⍵} )⍵}¨'foo' 'bar' 'foofoo'
1 0 1
The end
I hope this gives you a few ideas :)