Steven Taylor-3 wrote:
> 
> Hi,
> 
> as an exercise, I decided to make a simple "like" operator
> 
> like=:([:*./([=])+.('?'E.]))
> NB. in english: get membership flags where '?' is in the set y
> NB. OR that with the comparison flags
> NB. *./ insert-AND to get the result
> 
> the problem with this "like" operator is that x and y portions must be the
> same length.  For example:
>     'abcaabc' like 'abc?abc'
>   1
>     'Abcaabc' like 'abc?abc'
>   0
>     'abcaabc' like 'abc?ab'
>   |length error --- NB. fair enough that's how equals '=' is defined
> 
> Is there an easy way to do guard conditionally, but to do this tacitly?
> i.e.
> the lengths are different, so return fail/zero?
> [...]
> 

 like=:([:*./([=])+.('?'E.]))

First shorter and cuter version (parenthesis removed):

  like1 =: [: *./ = +. '?' E. ]

Then:

  like2 =: 0:`([: *./ = +. '?' E. ])@.(=&#)

   'abcabc' like2 'abc?bc'
1
   'abcabc' like2 'abc?bcc'
0


  
-- 
View this message in context: 
http://old.nabble.com/strings-tp31358507s24193p31358606.html
Sent from the J Programming mailing list archive at Nabble.com.

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to