I just started learning APL a couple of weeks ago, so this may sound like a newbie question.
Let B
be a string, which in APL terms can be either a scalor or a vector. If it's a scalar, ⍴B
returns null rather than the length of the string as I want.
B←'QR'
⍴B ⍝ returns 2
B←'Q'
⍴B ⍝ returns null
I discovered one way around that:
⍴1↓'X',B ⍝ concatenating X and then removing it returns a value of 1
That works, but it seems a little hokey, so I'm wondering if there is a more standard way to find string length.
Is it just me or does this seem a little inconsistent? The tutorial I read said to think of a scalar as a point similar to the way it is in vector algebra. But how is it that concatenating a scalar to a scalar makes a vector, but dropping a scalar from a vector never results in a scalar?
I'm really enjoying APL, so this question isn't meant as criticism. My question is, what's the best way to find string length? And, if anyone can shed a little light on this seeming inconsistency, it would be appreciated.
The reasons that concatenating X and removing it works, is that the catenation produces a vector. Removing X with 1↓
then leaves it as a vector, as an empty vector to be precise. And the length of vectors can be measured with ⍴. That is also what CrazyMetal's solution does: monadic ,
transforms its argument (scalar or array of any dimension) into a vector. And measuring its rho gives you the answer you were looking for: the standard way to find string length.
⍴,B
The behaviour that you see is caused by an inconsistency in the APL syntax. A sequence of characters enclosed in single-quotes creates an array of those characters. For example, 'foo'
creates an array of the characters f
, o
and o
. Calling ⍴
on this array shows the expected result:
⍴'foo'
3
However, these is an exception to this rule, and this is where the syntax is inconsistent. When you specify a single character inside the single quotes, you do not create an array of a single character, but rather the character itself is returned. This is why ⍴'f'
returns an empty array, since the value is a scalar.
As was suggested by Pé de Leão, you can use the ,
function to ensure that a scalar value becomes an array which is why using ⍴,
works regardless of whether its argument is a scalar or an array.
Because of this inconsistency, GNU APL has an extension that allows you to use "
instead of '
when typing strings. The only difference between them is that "
always creates an array, even for single characters.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With