use strict; use warnings; use Data::Dump; my %answers; my $question; while (my $line = ) { if ($line =~ / ^ (\d+) \. /x) { $question = $1; } elsif ($line =~ / ^ \* (\w+) \. \s* (.*?) \s* $ /x) { warn "Overwriting answer for question $question" if exists $answers{$question}; $answers{$question} = [$1, $2]; } } dd \%answers; __DATA__ 1. question? *a. answer begin end b. answer c. answer 2. question? a. answer *b. answer c. answer 3. question? a. answer b. answer *c. answer