'Raku array will not sort
I'm trying to sort a list/array of strings:
> my @e = Q (list_regex_files json_file_to_ref write_2d_array_to_tex_tabular dir get_sample_ID density_scatterplot violin_plot multiline_plot ref_to_json_file execute venn barplot scatterplot_2d_color worksheet_to_hash group_bar workbook_to_hash read_table)
using https://docs.raku.org/type/Array#(List)_routine_sort and I try
> say @e.sort
(list_regex_files json_file_to_ref write_2d_array_to_tex_tabular dir get_sample_ID density_scatterplot violin_plot multiline_plot ref_to_json_file execute venn barplot scatterplot_2d_color worksheet_to_hash group_bar workbook_to_hash read_table)
but
say <list_regex_files json_file_to_ref write_2d_array_to_tex_tabular dir get_sample_ID density_scatterplot violin_plot multiline_plot ref_to_json_file execute venn barplot scatterplot_2d_color worksheet_to_hash group_bar workbook_to_hash read_table>.sort
does work.
However, how can I save the data to an array and then sort it? like say @e.sort
?
Solution 1:[1]
to echo @Elizabeth s comment, dd is your friend ...
> my @a1 = Q (a b c); dd @a1; #Array @a1 = ["a b c"]
> my @a2 = <a b c>; dd @a2; #Array @a2 = ["a", "b", "c"]
here are the docs https://docs.raku.org/language/quoting for any passing readers
Solution 2:[2]
Q
is the base of the quoting domain specific sub language.
As such it is very bare bones, the only thing it does is a raw quote. You can't even escape the ending delimiter.
say Q (a b c \).raku
# "a b c \\"
You can enable extra features to that base language, like enabling :backslash
. (:b
is an alias)
say Q :backslash (a b c \)).raku
# "a b c )"
You probably thought that the :words
(:w
), or :quotewords
(:ww
) feature was enabled.
say Q :words (a b c).raku
# ("a", "b", "c")
say Q :quotewords (a b 'c d' "e f").raku
# ("a", "b", "c d", "e f")
Some of these features are useful enough that there are other ways to write them.
For example the :single
(:q
) feature enables backslashing the ending delimiter, but it is usually spelled written with just single quotes ' '
.
'a b c \' d' eqv Q :single 'a b c \' d' eqv Q:q 'a b c \' d'
There is another alias of q
for single quotes.
'a b c \' d' eqv q 'a b c \' d'
There is also a short cut which is spelled < >
.
<a b c> eqv Q :single :words (a b c) eqv Q:s:w (a b c)
Which is the one you thought you were using.
There are a lot more features that you can enable or disable. For a more comprehensive list and examples see the documentation.
Solution 3:[3]
this is a problem with the quoting construct.
If I use
my @e = <list_regex_files ...>
instead of my @e = Q (...)
then
@e.sort
works.
Solution 4:[4]
There are two issues here. First, if you insist on using the Q
quoting form, then you'll have to comb
or split
your string before sorting. Secondly, it's not clear you can guarantee your elements will be separated by one (and only one) whitespace character, meaning you'll have to handle 'variable whitespace' yourself:
my @f = Q (list_regex_files json_file_to_ref write_2d_array_to_tex_tabular dir get_sample_ID density_scatterplot violin_plot multiline_plot ref_to_json_file execute venn barplot scatterplot_2d_color worksheet_to_hash group_bar workbook_to_hash read_table); #variable whitespace
say @f.elems; #1
say @f.comb(/ \S+ /).elems; #17
say @f.split(' ', :skip-empty).elems; #17 with :skip-empty, 23 without
say @f.split(/ <.ws> /, :skip-empty).elems; #17 with skip-empty, 35 without
However, the result you desire is easily obtained starting from Q:w
('quote-word' quoting) instead of plain Q
:
my @g = Q:w (list_regex_files json_file_to_ref write_2d_array_to_tex_tabular dir get_sample_ID density_scatterplot violin_plot multiline_plot ref_to_json_file execute venn barplot scatterplot_2d_color worksheet_to_hash group_bar workbook_to_hash read_table); #variable whitespace
say @g.elems; #17
.raku.put for @g.sort;
OUTPUT:
"barplot"
"density_scatterplot"
"dir"
"execute"
"get_sample_ID"
"group_bar"
"json_file_to_ref"
"list_regex_files"
"multiline_plot"
"read_table"
"ref_to_json_file"
"scatterplot_2d_color"
"venn"
"violin_plot"
"workbook_to_hash"
"worksheet_to_hash"
"write_2d_array_to_tex_tabular"
https://docs.raku.org/language/quoting
https://docs.raku.org/routine/split
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | p6steve |
Solution 2 | Brad Gilbert |
Solution 3 | con |
Solution 4 |