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

# Lösung zu Blatt 2 --- UNIX Datenbanken II (WS 1999/2000)

## 2. Nomen Est Omen

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

open(FH, "ypcat passwd |") or die "Cannot open passwd\n";
while() {
@first = split / /, (split /:/)[4];
foreach \$first (@first[0..\$#first-1]) {
\$first{\$first}++;
}
}
close FH;
foreach \$name ((sort{\$first{\$b} <=> \$first{\$a}}(keys %first))[0..9]) {
print "\$name: \$first{\$name}\n";
}
```

## 3. Hangman

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

my \$file = shift || "/usr/dict/words";
open(FH, \$file) || die "Cannot open file \$file\n";
my \$anz = (split(" ", `wc -l \$file`))[0];
my (\$guess, \$sol, \$guesses, \$lines) = ("", "", 0, 0);
my %guessed = ();
\$lines = int(\$anz * rand);
while(defined(chomp(\$guess = )) && \$lines--){};
close FH;
my @letters = split(//, \$guess);

for (my \$i = 0; \$i <= \$#letters; \$i++) {
\$sol[\$i] = ".";
}
while (join('', @sol) ne \$guess) {
print join('', @sol)." tries \$guesses, guessed: ";
print((defined %guessed?join('', sort(keys(%guessed))):"")."\n");
print "Please enter a letter: ";
chomp(\$input = <>);
next unless length(\$input) == 1;
\$guessed{\$input} = 1; \$guesses++;
for (my \$i = 0; \$i <= \$#letters; \$i++) {
if (\$letters[\$i] eq \$input) {
\$sol[\$i] = \$input;
}
}
}
print join('', @sol)." tries \$guesses, guessed: ";
print((defined %guessed?join('', sort(keys(%guessed))):"")."\n");
print "You got it!\n";
```

Ingo Melzer, 09. November 1999