Universität Ulm, Fakultät für Mathematik und Wirtschaftswissenschaften, SAI

# Lösung zu Blatt 6 --- OO-Datenbank Anwendungen (WS 2002)

## Die Welt ist klein

```#!/usr/local/bin/perl -w

use strict;
use IO::File;

my %name; my (\$name, \$pers, \$knows, @knows, \$last); my \$met = {};
my \$file = shift || "huck.dat";
my \$fh = new IO::File \$file || die "Cannot open file \$file\n";
while(<\$fh>) {
chomp;
next if /^[ *#]/;
if (/^(\w+) (.*)/) { \$name{\$1} = \$2; }
if (/^(&|\d+[^:]*):(.*)/) {
\$knows = \$2;
unless (\$1 =~ /^&/) { @knows = (); }
push @knows, split(/;|,/, \$knows);
foreach \$pers (@knows) {
foreach \$knows (@knows) {
next if \$pers eq \$knows;
\$met->{\$pers}->{\$knows} = 1;
}
}
}
}
\$fh->close;
print "The following persons have been found: ";
print join(", ", sort keys %{\$met}).".\n";
print "Please enter the person's key: ";
chomp(\$name = <>);
die "Unknown person \$name" if !defined(\$name{\$name});
my (\$steps) = (0);
my %done = (); my (@more) = (\$name); my (@newmore) = ();
while (\$#more >= 0) {
print "\$name knows in \$steps steps: ";
foreach \$pers (sort @more) {
next if defined \$done{\$pers};
print "\$pers ";
push @newmore, (keys %{\$met->{\$pers}});
\$done{\$pers} = 1;
}
print "\n";
@more = @newmore; @newmore = ();
\$steps++;
}
```

Ingo Melzer, 11. Dezember 2002