Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Paths::Graph use

by zakishah (Novice)
on Aug 22, 2012 at 02:30 UTC ( #988926=perlquestion: print w/ replies, xml ) Need Help??
zakishah has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to use Paths::Graph in order to find shortest path between nodes, for this i am trying to access txt file to input data , my data contains ip address of node with metric value, but my code is showing error. My txt file is

(10.0.0.128 =>{10.0.0.129=>3,10.0.0.129=>4,10.0.0.130=>3,10.0.0.131=>2 +,10.0.0.132=>1,10.0.0.133=>1,10.0.0.134=>2,10.0.0.135=>4,10.0.0.136=> +1,10.0.0.137=>4,10.0.0.138=>3}, 10.0.0.129 =>{10.0.0.128=>3,10.0.0.130=>1,10.0.0.131=>2,10.0.0.132=>1, +10.0.0.133=>2,10.0.0.134=>1,10.0.0.135=>2,10.0.0.136=3,10.0.0.137=>3, +10.0.0.138=>4}, );

My code is

open FILE, '<', "data.txt" or die $!; my %graph = <FILE> use Paths::Graph; my $g = Paths::Graph->new(-origin=>"10.0.0.128"=>"10.0.0.138",-graph=> +\%graph); my @paths = $g->shortest_path(); for my $path (@paths) { print "Shortest Path:" . join ("->" , @$path) . " Cost:". $g-> +get_path_cost(@$path) ."\n"; }

Comment on Paths::Graph use
Select or Download Code
Re: Paths::Graph use
by Anonymous Monk on Aug 22, 2012 at 02:51 UTC
    And what is the error?
Re: Paths::Graph use
by Khen1950fx (Canon) on Aug 22, 2012 at 03:57 UTC
    Paths::Graph has a debug method that runs the gamut of the entire algorithm. Here's an example:
    #!/usr/bin/perl BEGIN { $| = 1; $^W = 1; } use autodie; use strictures 1; my %graph = ( A => { B => '10.0.0.128', C => '10.0.0.132', G => '10.0.0.129', }, C => { A => '10.0.0.135', B => '10.0.0.128', F => '10.0.0.134', }, ); use Paths::Graph; use strict qw/refs/; use warnings FATAL => 'syntax'; my $obj = Paths::Graph->new( -origin => "A", -destiny => "B", -graph => \%graph, ); $obj->debug(); my @paths = $obj->shortest_path(); foreach my $path (@paths) { print "Shortest Path:" . join( "->", @{$path} ) . " Cost:" . $obj->get_path_cost( @{$path} ); print "\n"; }
    Please note that your use of @${path} is incorrect. The correct way is @{$path}.
Re: Paths::Graph use
by RMGir (Prior) on Aug 22, 2012 at 11:52 UTC
    I don't think
    my %graph = <FILE>
    does what you think it does...

    I think that will get you a "%graph" hash with the first line as a key, the 2nd line as a value, and so on so forth...

    Try adding this right after you load %graph and see what it prints:

    foreach my $k(keys %graph) { print "<<<<<\n$k ===>>>> $graph{$k}\n>>>>>\n" }

    Mike

      Thank you very much , but my purpose is not to print file here, my purpose is to read file and calculate the shortest distance between desitantion and origin, which my code is not calculating the problem is not with path calculation here but it is in reading file. Please suggest on this issue.

        Why not structure your data as correct perl, then just read it in and eval it?
        # data file ( # Quoting the dotted quads since => will not quote # those correctly '10.0.0.128' => { '10.0.0.129'=>3, '10.0.0.129'=>4, '10.0.0.130'=>3, '10.0.0.131'=>2, '10.0.0.132'=>1, '10.0.0.133'=>1, '10.0.0.134'=>2, '10.0.0.135'=>4, '10.0.0.136'=>1, '10.0.0.137'=>4, '10.0.0.138'=>3 }, '10.0.0.129' => { '10.0.0.128'=>3, '10.0.0.130'=>1, '10.0.0.131'=>2, '10.0.0.132'=>1, '10.0.0.133'=>2, '10.0.0.134'=>1, '10.0.0.135'=>2, '10.0.0.136'=>3, # the > was missing on this line... '10.0.0.137'=>3, '10.0.0.138'=>4 }, );
        Now your reading code can simply be:
        my $x=join "",<FILE>; my %graph = eval $x;

        Mike

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://988926]
Approved by BrowserUk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2014-12-25 14:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (160 votes), past polls