Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: Replacing string with regexp without using modules

by cowgirl (Acolyte)
on Aug 06, 2008 at 00:37 UTC ( [id://702529]=note: print w/replies, xml ) Need Help??


in reply to Re: Replacing string with regexp without using modules
in thread Replacing string with regexp without using modules

Thanks for the great reply. It works well. However when I cut it down to this, it doesn't work:
$string = '$a $b $c'; $hash{a} = "go"; $string =~ s{\$(\w+)}{$$hash{$1}}ge;
Could it be the hash reference you use to the subroutine? \$hash ? Thanks.

Replies are listed 'Best First'.
Re^3: Replacing string with regexp without using modules
by almut (Canon) on Aug 06, 2008 at 01:58 UTC
    Could it be the hash reference ...?

    Yes, in pc88mxer's code, $hash is a hash reference, so it must be dereferenced. In your case, it's a normal hash.  So either don't dereference

    $hash{a} = "go"; $string =~ s{\$(\w+)}{$hash{$1}}ge;

    or create a reference

    $hash->{a} = "go"; $string =~ s{\$(\w+)}{$$hash{$1}}ge;

    or even use the normal arrow syntax in both the assignment $hash->{a} = ..., and when you access it $hash->{$1}:

    $hash->{a} = "go"; $string =~ s{\$(\w+)}{$hash->{$1}}ge;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2024-04-23 10:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found