in reply to Array of hashes of Arrays

This isn't going to answer your question, but why aren't you using Graph? That would seem to solve your real problem ...

Being right, does not endow the right to be rude; politeness costs nothing.
Being unknowing, is not the same as being stupid.
Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Replies are listed 'Best First'.
Re^2: Array of hashes of Arrays
by thekestrel (Friar) on Feb 21, 2005 at 16:53 UTC
    Dragonchild,

    I'm not using Graph because I haven't come across it before to be honest. Graph seems to provide dumping and manipulation routines, but ultimately I need this to be fast so routing through an external nodule isn't ideal (not that my code is the pinnacle of fast execution).

    I'm not sure what you mean by 'solve your real problem', but if you mean just ensuring that my data structure is sound to begin with I usually just data dump, and perhaps should have provided this in my initial question for further clarity.

    $VAR1 = { 'name' => 'I1', 'inputs' => '-' }; $VAR2 = { 'name' => 'I2', 'inputs' => '-' }; $VAR3 = { 'outputs' => '-', 'name' => 'O1' }; $VAR4 = { 'outputs' => '-', 'name' => 'O2' }; After we do an AddConnection("I1","O2") $VAR1 = { 'outputs' => [ 'O2' ], 'name' => 'I1', 'inputs' => '-' }; $VAR2 = { 'name' => 'I2', 'inputs' => '-' }; $VAR3 = { 'outputs' => '-', 'name' => 'O1' }; $VAR4 = { 'outputs' => '-', 'name' => 'O2', 'inputs' => [ 'I1' ] }; we have 2 elements to choose from we picked 0 : O1 we have 2 elements to choose from we picked 0 : I1

    This shows that the data structure is sound and that the Addconnection subroutine does it's thing...
    Any further Ideas?

    Regards Paul.
      but ultimately I need this to be fast so routing through an external nodule isn't ideal (not that my code is the pinnacle of fast execution).

      Get it working, then get it fast. You have no idea how fast Graph is or isn't until you have tried it and benchmarked it. In fact, it may be so fast that you have no idea how you lived without it.

      Further more, "routing through an external module" is no slower than using a module you wrote yourself. And, it may be faster for several reasons:

      • The author probably knows the problemspace better than you do, so they have the right algorithm(s).
      • The author probably has some caching mechanisms in place.
      • The author may have even rewritten critical parts in XS, providing up to a 100x speed improvement

      Try Graph. If it's not fast enough, I have a faster version that assumes one edge between each connected vertex. At least study the Graph code - at least it works.

      Being right, does not endow the right to be rude; politeness costs nothing.
      Being unknowing, is not the same as being stupid.
      Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
      Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

        Dragonchild

        I've rewritten it with Graph =), it now has more functionality with less code and is cleaner.. thanks again.


        Regards Paul
        use strict; use Graph; my $graph = Graph->new(); sub AddInput { my $name = $_[0]; $graph->set_vertex_attributes($name, { allow_inputs => "No", a +llow_outputs => "Yes" }); } sub AddOutput { my $name = $_[0]; $graph->set_vertex_attributes($name, { allow_inputs => "Yes", +allow_outputs => "No" }); } sub AddNeuron { my $name = $_[0]; $graph->set_vertex_attributes($name, { allow_inputs => "Yes", +allow_outputs => "Yes" }); } sub AddConnection { my ($from, $to, $weight) = @_; my $f = $graph->get_vertex_attribute($from, "allow_outputs"); my $t = $graph->get_vertex_attribute($to, "allow_inputs"); if ( $f eq "Yes" && $t eq "Yes" ) { $graph->add_weighted_edge($from,$to, $weight); } } AddInput("I1"); AddInput("I2"); AddNeuron("N1"); AddNeuron("N2"); AddNeuron("N3"); AddOutput("O1"); AddConnection("I1", "N1", int(rand(10)) ); AddConnection("I1", "N2", int(rand(10)) ); AddConnection("I1", "N3", int(rand(10)) ); AddConnection("I2", "N1", int(rand(10)) ); AddConnection("I2", "N2", int(rand(10)) ); AddConnection("I2", "N3", int(rand(10)) ); AddConnection("N1", "O1", int(rand(10)) ); AddConnection("N2", "O1", int(rand(10)) ); AddConnection("N3", "O1", int(rand(10)) ); print "$graph\n"; my @source = $graph->source_vertices(); foreach $a ( @source ) { print "source : $a\n"; }
        Dragonchild,
        You are of course correct...=)... Thanks for the link to the Graph module I'll look into it =)
        Regards Paul.