in reply to How to get ($1, $2, ...)?
Perhaps put your reg exes in a loop an array?
tinkered with the format of the output
update 2:
forgot to update the code. :-( Thanks to Tanktalus for spotting it.
output:#!/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
updated:$VAR1 = [ [ 'The Moor\'s Last Sigh', 'Salman', 'Rushdie' ], [ 'The God of Small Things', 'Arundhati', 'Roy' ], [ 'one', 'two', 'three', 'four' ], [ 'five', 'six', 'seven', 'eight', 'nine' ] ];
tinkered with the format of the output
update 2:
forgot to update the code. :-( Thanks to Tanktalus for spotting it.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: How to get ($1, $2, ...)?
by Anno (Deacon) on Feb 16, 2007 at 16:28 UTC | |
by Anno (Deacon) on Feb 16, 2007 at 21:47 UTC | |
Re^2: How to get ($1, $2, ...)?
by ferreira (Chaplain) on Feb 16, 2007 at 16:15 UTC | |
by varian (Chaplain) on Feb 16, 2007 at 16:35 UTC | |
by eric256 (Parson) on Feb 16, 2007 at 16:35 UTC | |
by Anno (Deacon) on Feb 16, 2007 at 16:40 UTC |
In Section
Seekers of Perl Wisdom