soo need write a sub what return the array of texts from the hashes, - in the dependent order, so from the above example need to get:

"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

as you can see, in the @arr can be some duplicated "lines", ("id2" in the above example), need output only once any id.

Not providing any code example yet, because havent any idea how to start. ;(
Exists some CPAN module what can be used to the solution?

Btw, I always assume my solution to something like this can be improved, but I don't really know. This is just what struck me off the top of my head.
–
zostayAug 28 '12 at 20:01

Now I need solve similar problem as OP asked. But I'm trying find a solution what is not depending on an BIG CPAN module, like Graph.pm. Testing your solution, but it is unfortunetely not works for the OP's example, and dies on "some requirements don't exists...". Can you please modify the code to prints the needed solution? (so, print all lines in the required order, and the "alone" lines (without deps) can be printed anywhere? Thay would be help me a lot. :)
–
kobameMay 4 '13 at 15:16

I would use a directed graph to represent the dependency tree and then walk the graph. I've done something very similiar using Graph.pm

Each of your hashes would be a graph vertex and the edge would represent the dependency.This has the added benefit of supporting more complex dependencies in the future as well as providing shortcut functions for working with the graph.

you didn't say what to do of the dependencies are "independent" of each other.

E.g. id1 requires id2; id3 requires id4; id3 requires id5. What should the order be? (other than 1 before 2 and 3 before both 4/5)

What you want is basically a BFS (Breadth First Search) of a tree (directed graph) of dependencies (or a forest depending on answers to #1 - the forest being a set of non-connected trees).

To do that:

Find all of the root nodes (ids that don't have a requirement themselves)

You can easily do that by making a hash of ALL the IDs using grep on your data structure

Put all those root modes into a starting array.

Then implement BFS. If you need help implementing basic BFS using an array and a loop in Perl, ask a separate question. There may be a CPAN module but the algorithm/code is rather trivial (at least once you wrote it once :)