Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
As I understand the question, it is how to store the regular expression in a variable (and use them later.)

Here is a quick example which uses a "junk" Apache log and IIS log (they each contain but three fields -- a number, a file, and a date.) You can work out the expressions for the real logs. *Smiles*

use strict; #-- Define variables my $mDate; my $mDoc; my $mFile; my $mNumber; my $mSystem = 'apache'; my @mDocuments = ( 'report.html', 'secret.html' ); my %mLogExpr = ( apache => '^(\d+)\s*(\S+)\s*(\S+)$', iis => '^(\S+)\s*(\S+)\s*(\d+)$' ); my %mTotalDocs; #-- Initialize accumulators foreach (@mDocuments) { $mTotalDocs{$_} = 0; } #-- Process the information while (<DATA>) { #-- Parse line and skip non-matching ones next if ! /$mLogExpr{$mSystem}/; #-- Store the parsed values if ($mSystem eq 'apache') { ($mNumber, $mFile, $mDate)=($1,$2,$3); } else { ($mFile, $mDate, $mNumber)=($1,$2,$3); } #-- Since I am not doing anything with the information, #-- I am going to print it. *Smiles* print "Number:\t", $mNumber, "\n"; print "File:\t", $mFile, "\n"; print "Date:\t", $mDate, "\n"; print "\n"; #-- Check for documents foreach $mDoc (@mDocuments) { $mTotalDocs{$mDoc}++ if /$mDoc/; } } #-- Print the results foreach (@mDocuments) { print "Total for $_: $mTotalDocs{$_}\n"; } __DATA__ #Apache Log 123 report.html 2001-09-16 123 report.html 2001-09-17 234 nothing.html 2001-09-18 123 report.html 2001-09-18 345 secret.html 2001-09-19 567 stuff.html 2001-09-20 # IIS log report.html 2001-09-16 123 report.html 2001-09-17 123 nothing.html 2001-09-18 234 report.html 2001-09-18 123 secret.html 2001-09-19 345 stuff.html 2001-09-20 567

You can switch the log back and forth (between apache and iis) by changing the one $mSystem variable.

Does this help?

Update

I forgot to mention this the first time, but I also noticed a couple of other things in your example:

  • The '/' is included within the expression string
  • When building an expression string, ' is better than " to make sure characters are not evaluated
  • The information for a line is only being saved if the line matched /$document/... when /$document2/ matches, the OLD information will be printed to the report

Update #2

I need to start using next unless instead of next if ! *Grins*


In reply to Re: question about regex and using a scalar variable to store/call it by Rhose
in thread question about regex and using a scalar variable to store/call it by Anonymous Monk

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (7)
    As of 2019-09-16 17:13 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      The room is dark, and your next move is ...












      Results (196 votes). Check out past polls.

      Notices?