$_= ~/^\w+\s*=\s*\w+\s*\,\?\w+/ ;

First thing: if you don't supply a variable, the regex will automatically work on $_, so you shouldn't include that. It's confusing and may lead to bugs. Just put the /regex/.

Second, your regex doesn't actually do anything. It checks the pattern against your chomped line, but it doesn't do anything to it. To manipulate it, you'd need to add another section and make it a s/// operator, with the replacement portion between the second and third delimiters. I don't know what you do want to do to it, so for now, here's what your pattern is checking for. I'm going to use the /x modifier so that I can include whitespace and comments, which I'd suggest you do in the future unless you're dealing with a simple enough regex that it's obvious at a glance what it does.

/^ # anchored to the beginning of the string \w+ # one or more word characters (a word) \s* # zero or more whitespace characters = # an equals sign \s* # zero or more whitespace characters \w+ # one or more word characters (a word) \s* # zero or more whitespace characters \, # a comma (you actually don't need to backslash this) \? # a question mark \w+ # one or more word characters (a word) /x;

As you can see now, this will require that a matching line include a comma followed by a question mark. That's probably not what you want. It also requires a third "word" following the question mark. A good method would be to write out exactly what you want to match, as I've done with my comments above, and then figure out the regex pattern piece by piece from there. Then explain what changes you want to make to it, and we can figure out the second part.

Aaron B.
Available for small or large Perl jobs and *nix system administration; see my home node.


In reply to Re^9: Need advice on checking two hashes values and keys by aaron_baugher
in thread Need advice on checking two hashes values and keys by perlynewby

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.