有一个哈希数组,
my @arr = get_from_somewhere();
@arr 内容(例如)是:
@arr = (
{ id => "id2", requires => 'someid', text => "another text2" },
{ id => "xid4", requires => 'id2', text => "text44" },
{ id => "someid", requires => undef, text => "some text" },
{ id => "id2", requires => 'someid', text => "another text2" },
{ id => "aid", requires => undef, text => "alone text" },
{ id => "id2", requires => 'someid', text => "another text2" },
{ id => "xid3", requires => 'id2', text => "text33" },
);
需要类似的东西:
my $texts = join("\n", get_ordered_texts(@arr) );
soo 需要编写一个 subtext
从哈希中返回 s 的数组, - 按照依赖顺序,所以从上面的例子需要得到:
"some text", #someid the id2 depends on it - so need be before id2
"another text2", #id2 the xid3 and xid4 depends on it - and it is depends on someid
"text44", #xid4 the xid4 and xid3 can be in any order, because nothing depend on them
"text33", #xid3 but need be bellow id2
"alone text", #aid nothing depends on aid and hasn't any dependencies, so this line can be anywhere
如您所见,@arr 中可以有一些重复的“行”,(上例中的“id2”),任何 id 只需要输出一次。
尚未提供任何代码示例,因为不知道如何开始。;( 存在一些 CPAN 模块可以用来解决什么问题?
谁能指出我正确的方向?