john.tm has asked for the wisdom of the Perl Monks concerning the following question:
Hi Monks, I have a script that pulls from a report regex matches. Then i wish to refernce these matches outside of the loop, but i do not know if it is possible to assign the matches to a variable.
As you can see in my example report. The book ref number and title are matched. so on which ever report the script is executed there will only ever be one result for each pattern, book ref no. and title , as each report has the same format. I was hoping to assign each match to $book and $title, for later reference.
#!/usr/bin/perl use strict; use warnings; my $book= "^\\s\*book ref \#"; my $book_res = $book =~ "/^\\s\*owner \#/"; my $title = "^\\s\*title "; my $title_res = $title =~ "/^\\s\*title /"; foreach (<DATA>) { next unless /$book|$title/ip; print ; } # here i would like to access the regex matches as scalers __DATA__ Book ref #4346 Lent: Sun Jul 12 03:26:43 BST 2015 status Lent Description: classic title blah blah blah last used: 2 color red Pages 238 Publisher Bca Type Hardback Location: N/a Author R jones
resulted matches Book ref #4346 title blah blah blah
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Perl store as variables regex matches for use outside of loop.
by 1nickt (Canon) on Jul 12, 2015 at 15:12 UTC | |
|
Re: Perl store as variables regex matches for use outside of loop.
by stevieb (Canon) on Jul 12, 2015 at 15:15 UTC | |
|
Re: Perl store as variables regex matches for use outside of loop.
by Laurent_R (Canon) on Jul 12, 2015 at 15:55 UTC | |
by 1nickt (Canon) on Jul 12, 2015 at 16:26 UTC | |
by Laurent_R (Canon) on Jul 12, 2015 at 17:50 UTC | |
|
Re: Perl store as variables regex matches for use outside of loop.
by davido (Cardinal) on Jul 12, 2015 at 16:52 UTC |