将 A 设置为排序数组 perl

getting A set into a sorted array perl

我一直在寻找一种将集合转换为排序数组的方法。

我找到了解决办法,但似乎有点荒谬:

use Set::Scalar;
Set::Scalar->as_string_callback(sub{join("\n", sort $_[0]->elements)});
#above formats print so that a line is added between each element
... #assume things are added to the set in this code
my @arr = split("\n", "$set"); #array contains each sorted element of the set 

我唯一的问题是,在我看来,这不应该是 fast/memory 执行此操作的有效方法。如果您有任何建议,请告诉我。先感谢您。

my @arr = sort $set->elements;