1. What is your name? 2. Please list your address and home phone number. 3. What is your mother's maiden name (name before marriage)? 4. Name your four favorite bands or artists:
use strict; use Data::Dumper; my $file = "questions.lst"; my $answer = <<EOT; 1. What is your name? Rich 2. Please list your address and home phone number. 12345 Sesame Street Footown, OH 45678 (123)555-12345 3. What is your mother's maiden name (name before marriage)? Arnold 4. Name your four favorite bands or artists: The Beatles Black Crowes Red Earth Matthew Sweet EOT ####################################################### # MAIN ####################################################### my @questions = &getQuestions($file); print Dumper(@questions); # Print Divider print qq(----------\nBEGIN TEST\n----------\n); # TEST # Making sure $answers matches all the questions foreach my $question(@questions) { if ($answer =~ m/$question/g) { print qq(TEST: \$answer matched $question\n); } else { print qq(TEST: \$answer did NOT match $question\n); } } # Print Divider print qq(----------\nEND TEST\n----------\n); # Loop trying to match current question and next question my $i; for($i = 0; $i < scalar(@questions); $i++) { my $j = $i + 1; # $j is the next element of the array print qq/Checking \$questions[$i] and \$questions[$j] \n/; print qq/Matching element $i\n/; if ($answer =~ m/\Q$questions[$i]\E/g) { print qq(Matched $questions[$i]\n); } else { print qq(Did NOT match $questions[$i]\n); } if ($questions[$j]) { if ($answer =~ m/\Q$questions[$j]\E/g) { print qq(Matched $questions[$j]\n); } else { print qq(Did NOT match $questions[$j]\n); } } print qq(-----------------------------\n); # Dividing line } sub getQuestions() { my $file = $_[0]; my @questions; open(FILE, "<$file") || die "Could not open $file: $!\n"; while(<FILE>) { s/^\s+//g; if (/^\d/) { chomp; push(@questions, $_); } } return @questions; }
$VAR1 = '1. What is your name?'; $VAR2 = '2. Please list your address and home phone number.'; $VAR3 = '3. What is your mother\'s maiden name (name before marriage)? +'; $VAR4 = '4. Name your four favorite bands or artists:'; ---------- BEGIN TEST ---------- TEST: $answer matched 1. What is your name? TEST: $answer matched 2. Please list your address and home phone numbe +r. TEST: $answer matched 3. What is your mother's maiden name (name befor +e marriage)? TEST: $answer matched 4. Name your four favorite bands or artists: ---------- END TEST ---------- Checking $questions[0] and $questions[1] Matching element 0 Did NOT match 1. What is your name? Matched 2. Please list your address and home phone number. ----------------------------- Checking $questions[1] and $questions[2] Matching element 1 Did NOT match 2. Please list your address and home phone number. Matched 3. What is your mother's maiden name (name before marriage)? ----------------------------- Checking $questions[2] and $questions[3] Matching element 2 Did NOT match 3. What is your mother's maiden name (name before marria +ge)? Matched 4. Name your four favorite bands or artists: ----------------------------- Checking $questions[3] and $questions[4] Matching element 3 Did NOT match 4. Name your four favorite bands or artists: -----------------------------
# Loop trying to match current question and next question my $i; for($i = 0; $i < scalar(@questions); $i++) { my $answer2 = $answer; # !!!NEW!!! my $j = $i + 1; # $j is the next element of the array print qq/Checking \$questions[$i] and \$questions[$j] \n/; print qq/Matching element $i\n/; if ($answer2 =~ m/$questions[$i]/g) { print qq(Matched $questions[$i]\n); } else { print qq(Did NOT match $questions[$i]\n); } if ($questions[$j]) { if ($answer2 =~ m/\Q$questions[$j]\E/g) { print qq(Matched $questions[$j]\n); } else { print qq(Did NOT match $questions[$j]\n); } } print qq(-----------------------------\n); # Dividing line }
$VAR1 = '1. What is your name?'; $VAR2 = '2. Please list your address and home phone number.'; $VAR3 = '3. What is your mother\'s maiden name (name before marriage)? +'; $VAR4 = '4. Name your four favorite bands or artists:'; ---------- BEGIN TEST ---------- TEST: $answer matched 1. What is your name? TEST: $answer matched 2. Please list your address and home phone numbe +r. TEST: $answer matched 3. What is your mother's maiden name (name befor +e marriage)? TEST: $answer matched 4. Name your four favorite bands or artists: ---------- END TEST ---------- Checking $questions[0] and $questions[1] Matching element 0 Matched 1. What is your name? Matched 2. Please list your address and home phone number. ----------------------------- Checking $questions[1] and $questions[2] Matching element 1 Matched 2. Please list your address and home phone number. Matched 3. What is your mother's maiden name (name before marriage)? ----------------------------- Checking $questions[2] and $questions[3] Matching element 2 Matched 3. What is your mother's maiden name (name before marriage)? Matched 4. Name your four favorite bands or artists: ----------------------------- Checking $questions[3] and $questions[4] Matching element 3 Matched 4. Name your four favorite bands or artists: -----------------------------
In reply to Matching inconsistency in for loop by Rich36
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |