Raku中存入Array的字符串列表调用sort无效如何解决
问题根因
你初始化数组的方式错误:Raku 中 Q (xxx) 是单引号字符串构造语法,生成的是单个完整字符串,赋值给数组 @e 后,整个数组仅包含1个元素,调用 sort 方法自然不会产生排序效果。而你直接调用的 <xxx>.sort 里的尖括号是「引号词列表构造器」,会自动按空白分割生成多元素列表,所以排序生效。
解决方法
调整数组初始化逻辑,把输入内容处理为多元素列表即可,两种常用方案:
- 直接用引号词构造器初始化数组
my @e = <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>;
- 如果你确实需要先通过
Q构造单字符串,再转成数组,可以手动按空白分割:
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).split(/\s+/);
完成初始化后,再执行 say @e.sort 就能得到预期的排序结果。
内容的提问来源于stack exchange,提问作者con
相关产品推荐
相关产品推荐

