User Tools

Site Tools


kurs:woerterbuch.pl
open (FH, "<woerterbuch.txt");

@zeilen = <FH>;

foreach $zeile (@zeilen) {
  #print "$zeile";
  #print $_;
  @worte = split("\;", $zeile);
  $wort{$worte[0]} = $worte[1];
  $worteng{$worte[1]} = $worte[0];
}

foreach $key (keys %wort) {
	print "$key $wort{$key}\n";
}

while ($eingabe ne "end") {
  print "Eingabe: ";
  $eingabe = <STDIN>;
  chomp ($eingabe);
  $eingabe = lc($eingabe);

  if(exists($wort{$eingabe})) {
    print $wort{$eingabe}."\n";
  } elsif (exists($worteng{$eingabe})) {
    print $worteng{$eingabe}."\n";
  }
  elsif ($eingabe ne "end") {
    print "Wort nicht vorhanden\n";
  }
}

Variante 2

use strict;
use warnings;

open (FH, "<woerterbuch.txt");

## my @lines = <FH>;
my %de2en;
my %en2de;
while (my $line = <FH>) {  
  my @woerter = split(';', $line);  
  # print "$woerter[0] $woerter[1] \n";
  $de2en{$woerter[0]} = $woerter[1];
#   $en2de{$woerter[1]} = $woerter[0];  
}

%en2de = reverse %de2en;

close FH;

my $search = $ARGV[0];

if (exists $de2en{$search}) {
   printf "Deutsch %s => Englisch %s\n",
      $search, $de2en{$search};
} elsif (exists $en2de{$search}) {
   printf "Englisch %s => Deutsch %s\n",
      $search, $en2de{$search};
} else {
   print "%s nicht gefunden\n", $search;
}      
kurs/woerterbuch.pl.txt · Last modified: 2014/09/10 21:22 (external edit)