Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I'd first recommend using some XML parser. Check CPAN for one that will easily handle the XML structure that you are looking at. Some are better than others for deep data structures, others are better at flatter data structures; some parse the entire document as a whole (loads the whole thing into memory), others parse it as you go. TMTOWTDI.

On a completely separate note, but attempting to pass on more general Perl wisdom, you have a couple of lines of code that literally make me cringe.

while ($record = <INPUTFILE>) { $_ = $record; if (/$searchString/g) { print $record; $matchesFound = $matchesFound + 1; } }
Here, you explicitly assign the next line in your file to a variable, then assign that variable to $_ (the default variable) so you can do a pattern match on the default variable and then print your explict variable. You only need one of these two variables. Since you do not need to do anything specific with $record, you could just as easily do this:
while (<INPUTFILE>) { ++$matchesFound && print if (/$searchString/); }
This will automatically assign to $_. print() will default to $_. You will increment $matchesFound and print $_ if $_ matches you pattern. Also note that you do not need the /g modifier since you do not need to find all matches, just any match.

Another way to do this (TMTOWTDI) is to assign the line to a real variable (as you do) but then use the =~ operator to pattern match against that variable:

while ($record = <INPUTFILE>) { if ($record =~ /$searchString/) { print $record; $matchesFound++; } }

Ivan Heffner
Sr. Software Engineer, DAS Lead
WhitePages.com, Inc.

In reply to Re: Pattern Matching Confusion by Codon
in thread Pattern Matching Confusion 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 all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (11)
    As of 2018-06-22 10:06 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?



      Results (124 votes). Check out past polls.

      Notices?