From 33613a85afc4b1481367fbe92a17ee59c240250b Mon Sep 17 00:00:00 2001 From: Sven Eisenhauer Date: Fri, 10 Nov 2023 15:11:48 +0100 Subject: add new repo --- .../examples/example11.pl | 62 ++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 Bachelor/Systemprogrammierung in Perl/examples/example11.pl (limited to 'Bachelor/Systemprogrammierung in Perl/examples/example11.pl') diff --git a/Bachelor/Systemprogrammierung in Perl/examples/example11.pl b/Bachelor/Systemprogrammierung in Perl/examples/example11.pl new file mode 100644 index 0000000..47e984b --- /dev/null +++ b/Bachelor/Systemprogrammierung in Perl/examples/example11.pl @@ -0,0 +1,62 @@ +#!/Perl/bin/perl +use strict; +use warnings; + +# Nun erfolgt die Initialisierung aus einem File in einem Unterprogramm + + +my ( %words ); +my ( $name, $guess ); + +&init_words; + +print "Wie heissen Sie? "; +$name = ; +chomp $name; + +if ( $name =~ /^gerhard\b/i ) { + print "Hallo Gerhard, wie nett, dass du da bist!\n"; +} +else { + print "Hallo $name!\n"; # Standard Gruß + print "Wie lautet das Geheimwort? "; + $guess = ; + chomp ( $guess ); + + while ( ! &good_word($name, $guess) ) { + print "Falsch geraten, nochmal. Wie lautet das Geheimwort? "; + $guess = ; + chomp $guess; + } +} +exit; + +sub init_words { + my ($word, $myname); + open(WORDSLIST, "wordlist.txt"); + while( $myname = ) { + chomp( $myname ); + $word = ; + chomp( $word ); + $words{ $myname } = $word; + } + close(WORDSLIST); +} + +sub good_word { + my ( $somename, $someguess ) = @_; #Parameter bennenen + + $somename =~ s/\W.*//; # alles hinter dem ersten Wort vergessen + $somename =~ tr/A-Z/a-z/; # alles in Kleinbuschstaben + + if( $somename eq "gerhard" ) { + return 1; #Rückgabewert ist wahr + } + elsif ( ($words{$somename} || "groucho") eq $someguess ) { + return 1; #Rückgabewert ist wahr + } + else { + return 0; #Rückgabewert ist falsch + } +} + -- cgit v1.2.3