Re: Passing hash ref [ was Re: finding head and tail in data structure

2013-01-11 Thread budi pearl
Hi Jim,

On Fri, Jan 11, 2013 at 1:54 PM, Jim Gibson  wrote:

> OR
>
> print_path( $id, $routes->{$id} );
> ...
> sub print_path
> {
> ...
> while( my ($start, $end) = each %$edges ) {
>
>
This is what i want, thank you!
Apparently  passing "$routes->{$id}" is my earlier issue.


--budi


Re: Passing hash ref [ was Re: finding head and tail in data structure

2013-01-10 Thread Jim Gibson

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/




Re: Passing hash ref [ was Re: finding head and tail in data structure

2013-01-10 Thread budi pearl
On Fri, Jan 11, 2013 at 11:05 AM, budi pearl  wrote:

> Hi Shawn,
>
> When trying to accessed inside subroutine , i got:
>
> Type of arg 1 to each must be hash (not hash element) at ./print_path.plline 
> 41, near "}) "
>
> Execution of ./print_path.pl aborted due to compilation errors.
>
>
> this is work:
>   while (my ($start, $end) = each %{$routes{$label}}) {
>
> but this not:
> while (my ($start, $end) = each %{$edges{$label}}) {
>
>
>
This will work, but this will pass %routes instead of %routes{$id}. Or it
should be that way?

my $hroute = \%routes;
print_path($id, $hroute);

...
while (my ($start, $end) = each %{$edges->{$label}}) {


Re: Passing hash ref [ was Re: finding head and tail in data structure

2013-01-10 Thread budi pearl
Hi Shawn,

When trying to accessed inside subroutine , i got:

Type of arg 1 to each must be hash (not hash element) at
./print_path.plline 41, near "}) "
Execution of ./print_path.pl aborted due to compilation errors.


this is work:
  while (my ($start, $end) = each %{$routes{$label}}) {

but this not:
while (my ($start, $end) = each %{$edges{$label}}) {

Thanks.

--budhi



On Fri, Jan 11, 2013 at 10:47 AM, Shawn H Corey wrote:

> On Fri, 11 Jan 2013 10:33:02 +0700
> budi pearl  wrote:
>
> > my $id = "ROUTE-252";
> > print Dumper $routes{$id};
> >
> > print_path($id, \%{$routes{$id}});
>
> I think you want:
>
>   print_path( $id, $routes{$id} );
>
>
> --
> Don't stop where the ink does.
> Shawn
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


Re: Passing hash ref [ was Re: finding head and tail in data structure

2013-01-10 Thread Shawn H Corey
On Fri, 11 Jan 2013 10:33:02 +0700
budi pearl  wrote:

> my $id = "ROUTE-252";
> print Dumper $routes{$id};
> 
> print_path($id, \%{$routes{$id}});

I think you want:

  print_path( $id, $routes{$id} );


-- 
Don't stop where the ink does.
Shawn

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Passing hash ref [ was Re: finding head and tail in data structure

2013-01-10 Thread budi pearl
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;
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}});

sub print_path {
my ($label, $edges) = @_;
my $graph = Graph::Directed->new;

while (my ($start, $end) = each $edges{$label}) {
#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";
}
}


}