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

In reply to Perl store as variables regex matches for use outside of loop. by john.tm

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.