Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^2: Hash of Regex

by Anonymous Monk
on Apr 06, 2010 at 22:46 UTC ( #833144=note: print w/ replies, xml ) Need Help??


in reply to Re: Hash of Regex
in thread Hash of Regex

my %dict = ( 'brown' => 'yellow', qr/int(\d+)/ => 'int', qr/float(\d+)/ => 'float', );
This is the output, but what does it mean and why doesn't it work?
$VAR1 = { '(?-xism:int(\\d+))' => 'int', 'brown' => 'yellow', '(?-xism:float(\\d+))' => 'float' };
This looks more elegant than this.
my %dict = ( 'brown' => 'yellow', 'int\(\d+\)' => 'int', 'float\(\d+\)' => 'float', );


Comment on Re^2: Hash of Regex
Select or Download Code
Re^3: Hash of Regex
by crashtest (Curate) on Apr 06, 2010 at 23:23 UTC

    Only strings can be hash keys. So when building %dict with regular expressions (the qr// constructs), they are converted to strings to become keys in the hash1. For regular expressions, incidentally, these are strings that can be compiled back into the same regular expression they stemmed from - which is quite convenient.

    So why did this not do anything to bichonfrisee74's input strings?

    my %dict = ( 'brown' => 'yellow', qr/int(\d+)/ => 'int', qr/float(\d+)/ => 'float', );
    I was scratching my head myself until I use'd re 'debug'. zwon happened to forget to escape the parentheses in his (intermediary) example. So if you use the (correctly written) qr// constructs below, things "work" as expected:
    my %dict = ( 'brown' => 'yellow', qr/int\(\d+\)/ => 'int', qr/float\(\d+\)/ => 'float', );


    1The ?-xism used to confuse me until I realized this just explicitly states: turn off the x, i, s and m regex flags.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2014-10-25 03:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (141 votes), past polls