in reply to Reading a File by Line then Analyzing each Line

Building your question sheet in a hash seems to be the way to go. The following code reads the quiz into a hash then reprints the form slightly reformatted.

use strict; use warnings; my %questions; my $question = 0; while (<DATA>) { chomp; my ($prefix, $text, $tag) = /^(\w+)\.\s*((?:(?!\*$).)*)(\*)?$/; next if ! defined $text; if ($prefix =~ /^\d+/) { $question = $prefix; $questions{$question}{Question} = $text; } else { push @{$questions{$question}{Answers}}, "$prefix. $text"; $questions{$question}{Answer} = "$prefix. $text" if defined $t +ag; } } # Print the question sheet for my $num (sort keys %questions) { print "$num. $questions{$num}{Question}\n"; for (@{$questions{$num}{Answers}}) { print " $_"; print " (answer)" if $_ eq $questions{$num}{Answer}; print "\n"; } } __DATA__ 1. What is my name? A. Rooney * B. Gerrard C. Ronaldo D. Ronaldinho 2. Who's your bet in the coming World Cup? A. Brazil * B. Germany C. England D. Cameroon

Prints:

1. What is my name? A. Rooney (answer) B. Gerrard C. Ronaldo D. Ronaldinho 2. Who's your bet in the coming World Cup? A. Brazil (answer) B. Germany C. England D. Cameroon

DWIM is Perl's answer to Gödel