http://www.perlmonks.org?node_id=600503


in reply to Rewrite for "How to get ($1, $2, ...)?
in thread How to get ($1, $2, ...)?

I don't know if this will help or not, but if you split it line by line and then trigger a new book every time you see the title, you get the same scanner like behavior without using /mgc. If you might have multiple fields in one line then it might be hard to use, but maybe some combination of the two methods would let you find a book and then use the @fields = $str =~ /$re/mgs code on just one book section at a time. For all I know you might be able to split on a boundary before "Title" and then have each book as a chunk to then run your multiple regexs on without fear of them leaking over to the next book. Good Luck! ;)

use strict; use warnings; use Data::Dumper; my $test =<<HERE; Title: The Moor's Last Sigh Author: Salman Rushdie Publisher: Foo asdf asdf asdf a d f d a sf as Title: The God of Small Things Author: Arundhati Roy Publisher: Bar HERE my @lines = split /\n/, $test; my $re_title = qr/Title: (.*?)$/; my $re_author = qr/Author: (\w+) (\w+)$/; my $re_publisher = qr/Publisher: (.*?)$/; my @answers; my $book; for my $line (@lines) { if ($line =~ /$re_title/) { #if this is a title line then the previous book is done being +scanned # so push the previous book onto answers and and clear out %bo +ok push @answers, $book if $book; $book = {}; $book->{title} = $1; } elsif ($line =~ /$re_author/) { $book->{author} = [ $1, $2 ]; } elsif ($line =~ /$re_publisher/) { $book->{publisher} = $1; } } #push the final book push @answers, $book; print Dumper(\@answers);

___________
Eric Hodges