On Jan 10, 2013, at 7:33 PM, budi pearl wrote: > Hi All, > > I would like to pass hash: %{$routes{"ROUTE-252"}} instead of %routes but > got this error: > > [budi@dev bin]$ ./print_path.pl > Type of arg 1 to each must be hash (not hash element) at > ./print_path.plline 38, near "}) " > Execution of ./print_path.pl aborted due to compilation errors. > > ---- > #use strict;
If you had not commented out this line, Perl would have told you what the problem is. > use Graph::Directed; > use Data::Dumper; > > my %routes = ( > "ROUTE-252" => { > # src => dest > CX77 => "ABEP", > ABEP => 441, > 441 => 427, > 427 => 444, > 444 => "MGWQ", > MGWQ => "CDEF" > }, > > "ROUTE-432" => { > AAA => "BBB", > BBB => "CCC", > CCC => "DDD", > DDD => "EEE", > EEE => "FFF", > XXX => "YYY", > YYY => "ZZZ", > } > ); > > my $id = "ROUTE-252"; > print Dumper $routes{$id}; > > print_path($id, \%{$routes{$id}}); You are passing a reference to the inner hash, but you are doing it by first de-referencing, then referencing the inner hash. This can be simply: print_path( $id, $routes{$id} ); > > sub print_path { > my ($label, $edges) = @_; You are storing the hash reference in the scalar variable $edges. This value is $routes{'ROUTE-252'}. > my $graph = Graph::Directed->new; > > while (my ($start, $end) = each $edges{$label}) { There are two problems here: 1. $edges{$label} is a member of the hash %edges, but you have not declared or defined %edges in your subroutine. 2. You are trying to access the member $route{'ROUTE-252'}->{'ROUTE-252'}. In other words, you are indexing twice. You should either pass the top-level hash and index in the subroutine, or pass the indexed hash member and do not index in the subroutine: print_path( $id, \%routes ); ... sub print_path { ... while( my ($start, $end) = each %{$edges->{$label}} ) { OR print_path( $id, $routes->{$id} ); ... sub print_path { ... while( my ($start, $end) = each %$edges ) { > #while (my ($start, $end) = each %{$routes{$label}}) { > $graph->add_edge($start, $end); > } > > my @sinks = $graph->sink_vertices; > for my $source ($graph->source_vertices) { > for my $sink (grep $graph->is_sink_vertex($_), > $graph->all_successors($source)) { > print "$label: ", join ' - ', $graph->path_vertices($source, > $sink); > print "\n"; > } > } > > > } -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/