phaylon, Good spot on the typo, its wasn't ultimately the main cause, but I think its happy now =).
Program....
use strict; use Data::Dumper; my @inputlist; my @outputlist; my @list; sub AddInput { my $name = $_[0]; # push @list, { name => $name, inputs => "-" }; push @list, { name => $name }; push @outputlist, $name; } sub AddOutput { my $name = $_[0]; # push @list, { name => $name, outputs => "-" }; push @list, { name => $name }; push @inputlist, $name; } sub AddNeuron { my $name = $_[0]; push @list, { name => $name }; push @inputlist, $name; push @outputlist, $name; } sub AddConnection { my ($from, $to) = @_; for my $l ( @list ) { if ( $l->{"name"} eq $from ) { push @{$l->{"outputs"}}, $to; } if ( $l->{"name"} eq $to ) { push @{$l->{"inputs"}}, $from; } } } sub AddRandomConnection { my @arr; my $i = scalar(@inputlist); print "we have $i elements to choose from\n"; my $rand_i = int(rand($i)); my $from = $inputlist[$rand_i]; print "we picked $rand_i : $from\n"; my $o = scalar(@outputlist); print "we have $i elements to choose from\n"; my $rand_o = int(rand($o)); my $to = $outputlist[$rand_o]; print "we picked $rand_o : $to\n"; #TODO: need to make sure you can't come to and from same node for my $l ( @list ) { if ( $l->{"name"} eq $from ) { push @{$l->{"outputs"}}, $to; } if ( $l->{"name"} eq $to ) { push @{$l->{"inputs"}}, $from; } } } AddInput("I1"); AddInput("I2"); #AddInput("I3"); AddOutput("O1"); AddOutput("O2"); #AddNeuron("N1"); #AddNeuron("N2"); print Data::Dumper->Dump([@list]); print "Add a direct connection\n"; AddConnection("I1", "O2"); print Data::Dumper->Dump([@list]); print "Add a random connection\n"; AddRandomConnection(); print Data::Dumper->Dump([@list]);

Gives output...
$VAR1 = { 'name' => 'I1' }; $VAR2 = { 'name' => 'I2' }; $VAR3 = { 'name' => 'O1' }; $VAR4 = { 'name' => 'O2' }; Add a direct connection $VAR1 = { 'outputs' => [ 'O2' ], 'name' => 'I1' }; $VAR2 = { 'name' => 'I2' }; $VAR3 = { 'name' => 'O1' }; $VAR4 = { 'name' => 'O2', 'inputs' => [ 'I1' ] }; Add a random connection we have 2 elements to choose from we picked 1 : O2 we have 2 elements to choose from we picked 1 : I2 $VAR1 = { 'outputs' => [ 'O2' ], 'name' => 'I1' }; $VAR2 = { 'name' => 'I2', 'inputs' => [ 'O2' ] }; $VAR3 = { 'name' => 'O1' }; $VAR4 = { 'outputs' => [ 'I2' ], 'name' => 'O2', 'inputs' => [ 'I1' ] };


Regards Paul

In reply to Re^2: Array of hashes of Arrays by thekestrel
in thread Array of hashes of Arrays by thekestrel

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.