Farid Hajji: Perl - Einführung, Anwendungen, Referenz
2., aktualisierte und erweiterte Auflage
Addison-Wesley Longman, ISBN 3-8273-1535-2
hoh.pl
#!/usr/local/bin/perl -w
# hoh.pl -- Hash von Hashes: Duenn besetzte Matrizen
use strict 'refs';
use Data::Dumper;
use Getopt::Std;
use vars qw ($opt_u $opt_v);
getopts("uv") || die "usage: $0 [-u] [-v]\n\n" .
" -u: undirected graph\n" .
" -v: verbose\n";
# Wir lesen eine Graphbeschreibung ein und erzeugen daraus
# eine duenn besetzte Matrix:
open(GRAPH, "hoh.data") or die "can't open hoh.data: $!\n";
while (<GRAPH>) {
chomp;
($from, $to, $weight) = split(/:/);
$p->{$from}{$to} = $weight;
$p->{$to}{$from} = $weight if defined $opt_u; # ungerichtet
}
close GRAPH;
# Hier zeigen wir die Struktur an:
print Dumper($p) if defined $opt_v;
# Einige typische Anfragen:
print "Node? "; chomp($node = <STDIN>);
@outgress = sort keys %{$p->{$node}};
foreach my $neighbor (@outgress) {
print "Distance to: $neighbor: ", d($node, $neighbor), "\n";
}
# Die Kantengewichtsfunktion: Liefert undef oder d(x,y),
# wobei undef bedeutet: Keine Kante von x nach y
# und d(x,y) das Gewicht der Kante von x nach y bedeutet.
sub d {
my ($x, $y) = @_;
return if not exists $p->{$x};
return if not exists $p->{$x}{$y};
return $p->{$x}{$y};
}
[Prev] [Up] [Relevant Chapter] [Next]
[Alte Quelle]
| Last modified: $Date: 2006/05/18 12:55:57 $ FH. Search :: Sitemap :: Disclaimer :: Copyright :: Privacy |
|