Thread Unmatched [ in regex (14 answers)
Opened by Gast at 2007-09-07 01:09

topeg
 2007-09-07 08:27
#99175 #99175
User since
2006-07-10
2611 Artikel
BenutzerIn

user image
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
sub GetQuestion {
         my $PointValue=shift(@_);
         my $TotalFiles=shift(@_);
         my $QuestionDBinfo=shift(@_);

         if (rand(15) == 0) { $PointValue *= 2; }
         my $FileNum = int(rand($TotalFiles))+1;
         my $QNum = int(rand($$QuestionDBinfo{$FileNum}[1]))+1;
         my $QuestionLine; # hier war der Hauptfehler "$QuestionLine" existierte nur innerhalb von "for"
         open(QFILE, $$QuestionDBinfo{$FileNum}[0]) or die "Error opening the trivia file\n";
         for($i = 0; $i < $QNum; $i += 1) {
                 $QuestionLine = <QFILE>;
         }
         close(QFILE);
         die "No string read from the trivia file!" if ($QuestionLine eq '');
         # Trenner ermitteln?
         my $d = substr($QuestionLine,0,1);
         # was machst du hier???
         my $Question = (split(/[$d][$d]/,$QuestionLine))[1];
         my $Category = (split(/[$d]/,$QuestionLine))[1];
         my $Answer = (split(/[$d]/,$QuestionLine))[2];
         # so wie das da steht macht es keinen Sinn.
         # gib mal eine Beispielzeile.
         # wenn ich den Code richtig interpretiere sieht eine Questionline so aus:
         # ;Kategorie;Frage;Antwort
         # Ein korrekter Trenner sähe wohl so aus:
         # my ($Category,$Question,$Answer)=(split($d,$QuestionLine))[1,2,3];
         my $QID = "F".$FileNum.".Q".$QNum;
         chomp($Question);
         if ($Category eq "Scramble") {
                 my @letters = ();
                 for($i=0;$i<length($Question);$i+=1) {
                         push(@letters,substr($Question,$i,1));
                 }
                 @letters = shuffle(@letters);
                 $Question = "";
                 for($i=0;$i<scalar(@letters);$i+=1) {
                         $Question = $Question." ".$letters[$i];
                 }
                 $Question = "Unscramble this word: ".$Question;
                 my $l = length($Answer) - 5;
                 while ($l >= 0) {
                         if (int(rand(2)) == 1) { $PointValue *= 2; }
                         $l -= 3;
                 }
         }
 
         my @retval = ($PointValue, $QID, $Category, $Question, $Answer);
 
         return @retval;
 }

# Aufruf:
@list=GetQuestion($ScoreValues[rand($#ScoreValues)],$TotalFiles,\@QuestionDBinfo);

Nebenbei: Globale Variablen sind des Teufels! ;-)

View full thread Unmatched [ in regex