I have the following string:
s=:'when can we dance'
I can find the length of each word with this:
# each ;:s
I can sort the lengths in ascending/descending order:(/:~) # each ;:s
which gives me boxed output.
But how do I get the words printed?
You're already using /:~
which is the Reflex of dyadic Sort Up; /:~ y
is equivalent to y /: y
. This is a convenient shortcut, but it this case what you're skipping past is the solution you're looking for. /:
(without Reflex) permutes its x
according to the permutation that would result in a sorted y
. That "permutation that would result in a sorted y
" is incidentally what monadic /:
(Grade Up) is for.
]w=:;:'when can we dance'
+----+---+--+-----+
|when|can|we|dance|
+----+---+--+-----+
/: w
1 3 2 0
(/: w) { w
+---+-----+--+----+
|can|dance|we|when|
+---+-----+--+----+
w /: w
+---+-----+--+----+
|can|dance|we|when|
+---+-----+--+----+
So to sort w
by something else, like the lengths of its items, just provide that something else as Sort Up's y
:
w /: # each w
+--+---+----+-----+
|we|can|when|dance|
+--+---+----+-----+
(/: # each) w NB. a hook
+--+---+----+-----+
|we|can|when|dance|
+--+---+----+-----+
;:
gives you the J words in a string, which might not be what you want:
;: 'when can''t we dance'
|open quote
| ;:'when can''t we dance'
One of the ;.
Cut family or stdlib's splitstring
or regexes might help.
13 : ';: ''when can we dance''' NB. 13 : can be a mnemonic
(<;._1 ' when can we dance')"_
<;._1 ' ','when can''t we dance'
+----+-----+--+-----+
|when|can't|we|dance|
+----+-----+--+-----+
' ' splitstring 'when can''t we dance'
+----+-----+--+-----+
|when|can't|we|dance|
+----+-----+--+-----+
s=:'when can''t we dance' NB. excess space
<;._1 ' ',s NB. empty boxes in result
+----+-----+++--+-----+
|when|can't|||we|dance|
+----+-----+++--+-----+
require 'regex'
'[\w'']+' rxall s
+----+-----+--+-----+
|when|can't|we|dance|
+----+-----+--+-----+
To get these word-lists back into a string,
w
+----+---+--+-----+
|when|can|we|dance|
+----+---+--+-----+
;: inv w
when can we dance
' ' joinstring w
when can we dance
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