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

Re: Hash of Regex

by zwon (Abbot)
on Apr 06, 2010 at 21:59 UTC ( [id://833135]=note: print w/replies, xml ) Need Help??


in reply to Hash of Regex

Try to check content of the hash:

use strict; my $test = 'The brown fox int(10) over float(200) fence.'; my %dict = ( 'brown' => 'yellow', /int(\d+)/ => 'int', /float(\d+)/ => 'float', ); use Data::Dumper; warn Dumper \%dict; __END__ $VAR1 = { 'int' => 'float', 'brown' => 'yellow' };
And if you'd use warnings...

Perhaps you also will be interested in:

my %dict = ( 'brown' => 'yellow', qr/int(\d+)/ => 'int', qr/float(\d+)/ => 'float', );

Update: And what you actually want, if I got you right, is:

use strict; my $test = 'The brown fox int(10) over float(200) fence.'; my %dict = ( 'brown' => 'yellow', 'int\(\d+\)' => 'int', 'float\(\d+\)' => 'float', ); for my $i ( keys %dict ) { $test =~ s/$i/$dict{$i}/gi; } print "$test\n";

Replies are listed 'Best First'.
Re^2: Hash of Regex
by almut (Canon) on Apr 06, 2010 at 22:19 UTC
    $VAR1 = { 'int' => 'float', 'brown' => 'yellow' };

    (note to the OP)  In case you wonder why this is:

    my %dict = ( 'brown' => 'yellow', /int(\d+)/ => 'int', /float(\d+)/ => 'float', );

    is the same as

    my %dict = ( 'brown' => 'yellow', $_ =~ /int(\d+)/ => 'int', $_ =~ /float(\d+)/ => 'float', );

    which (in this case) is the same as

    my %dict = ( 'brown' => 'yellow', () => 'int', () => 'float', );

    which is the same as

    my %dict = ( 'brown' => 'yellow', 'int' => 'float', );
      Why / how did this
      my %dict = ( 'brown' => 'yellow', () => 'int', () => 'float', );
      become this??
      my %dict = ( 'brown' => 'yellow', 'int' => 'float', );
        For Perl => is just a comma, and empty lists () are ignored when flattening lists.

        perl -e' @a=(1,2,(),(),3);print "@a"' 1 2 3

        Cheers Rolf

        Empty elements (lists) in a list collapse,  e.g. (1, (), 2, 3, (), (), 4) is the same as (1, 2, 3, 4).

Re^2: Hash of Regex
by Anonymous Monk on Apr 06, 2010 at 22:46 UTC
    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', );

      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.

Re^2: Hash of Regex
by 7stud (Deacon) on Apr 06, 2010 at 22:22 UTC

    Perhaps you also will be interested in:</p.

    my %dict = ( 'brown' => 'yellow', qr/int(\d+)/ => 'int', qr/float(\d+)/ => 'float', );
    That doesn't do anything for me.

    Edit: Woohoo. More negative rep. Keep it coming. I'm shooting for -100,000 by next week.
      That doesn't do anything for me.
      That shouldn't. OP asked if he can use regexp as a hash key, so I think he may be interested in inspecting content of mentioned hash with the help of Data::Dumper
        Thanks, I should have first checked the content of my hash using Data::Dumper.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-04-26 03:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found