Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^9: Need advice on checking two hashes values and keys

by aaron_baugher (Curate)
on Jun 10, 2015 at 00:37 UTC ( #1129763=note: print w/replies, xml ) Need Help??


in reply to Re^8: Need advice on checking two hashes values and keys
in thread Need advice on checking two hashes values and keys

$_= ~/^\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.

Replies are listed 'Best First'.
Re^10: Need advice on checking two hashes values and keys
by AnomalousMonk (Bishop) on Jun 10, 2015 at 02:21 UTC
    $_= ~/^\w+\s*=\s*\w+\s*\,\?\w+/ ;

    Actually, it's worse than that. Because the  = ~ is screwed up (should have been =~), the statement (if that's the statement that's actually in perlynewby's code (if that's actually perlynewby)) is assigning the 1s-complement of the numeric value of the result of the  m// against  $_ to  $_ instead:

    c:\@Work\Perl\monks>perl -wMstrict -le "$_ = 'x=x,?x'; print qq{'$_'}; ;; $_= ~/^\w+\s*=\s*\w+\s*\,\?\w+/ ; print qq{'$_'}; ;; $_ = '----'; print qq{'$_'}; ;; $_= ~/^\w+\s*=\s*\w+\s*\,\?\w+/ ; print qq{'$_'}; " 'x=x,?x' '4294967294' '----' '4294967295'


    Give a man a fish:  <%-(-(-(-<

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1129763]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2020-06-02 08:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?



    Results (15 votes). Check out past polls.

    Notices?