use strict; use warnings; my %questions; my $question = 0; while () { 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 $tag; } } # 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 #### 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