Perhaps put your reg exes in
a loop an array?
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my @res = (
qr/Title: (.*?), Author: (\w+) (\w+)$/,
qr/Title: (.*?), Author: (\w+) (\w+) Publisher: (\w+)$/,
qr/Title: (.*?), Author: (\w+) (\w+) Publisher: (\w+) Year: (\w+)$/,
);
my @answers;
while (my $line = <DATA>){
for my $re (@res){
my @results;
if (@results = $line =~ /$re/){
push @answers, [@results];
}
}
}
print Dumper \@answers;
__DATA__
Title: The Moor's Last Sigh, Author: Salman Rushdie
Title: The God of Small Things, Author: Arundhati Roy
Title: one, Author: two three Publisher: four
Title: five, Author: six seven Publisher: eight Year: nine
output:
$VAR1 = [
[
'The Moor\'s Last Sigh',
'Salman',
'Rushdie'
],
[
'The God of Small Things',
'Arundhati',
'Roy'
],
[
'one',
'two',
'three',
'four'
],
[
'five',
'six',
'seven',
'eight',
'nine'
]
];
updated:
tinkered with the format of the output
update 2:
forgot to update the code. :-( Thanks to
Tanktalus for spotting it.