
unless (@ARGV == 2) { die "Usage: $0 fieldname value\n" }
($field, $value) = @ARGV;

unless (open DATABASE, "<", "db.txt") {
  die "Couldn't open database db.txt: $!; aborting";
}
$firstline = <DATABASE>;
chomp $firstline;
@fieldnames = split /:/, $firstline;

for $n (0 .. $#fieldnames) {
  if ($field eq $fieldnames[$n]) {
    $fieldnumber = $n; last;
  }
}

unless (defined $fieldnumber) {
  die "Unrecognized field name `$field'\n";
}

while (<DATABASE>) {
  chomp;
  @fields = split /:/;
  if ($fields[$fieldnumber] eq $value) {
    print $_, "\n" ;
  }
}

