Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Replacing string with regexp without using modules

by cowgirl (Acolyte)
on Aug 05, 2008 at 22:34 UTC ( [id://702515]=perlquestion: print w/replies, xml ) Need Help??

cowgirl has asked for the wisdom of the Perl Monks concerning the following question:

I am currently using this code to replace $a and $b variables from a template. However I need to make this more dynamic so I do not need to specify the values any more.
sub prog_replace_string { my $replace_this = shift; my $with_this = shift; my $string = shift; my $length = length($string); my $target = length($replace_this); for(my $i=0; $i<$length - $target + 1; $i++) { if(substr($string,$i,$target) eq $replace_this) { $string = substr($string,0,$i) . $with_this . substr($stri +ng,$i+$target); } } return $string; }
I was wondering how to do replace certain denoted names dynamically in a template file, such as in this context: <b><u><i>$var</i></u></b> and replacing this with the value of $var. I would think this would be possible by using a reference to the regexp match in a regexp like: $string = s/\$XXXXXXXX/${$1}/; Of course this does not work, I do not know what to replace XXXXXXXX with and the $1 variable would return the entire variable name, however we need it without the $ sign. This must be really easy. I would love if someone could help me out without pointing to the use of a module. Thanks a lot!

Replies are listed 'Best First'.
Re: Replacing string with regexp without using modules
by pc88mxer (Vicar) on Aug 05, 2008 at 22:51 UTC
    For your first question:
    sub prog_replace_string { my ($replace_this, $with_this, $string) = @_; $string =~ s{\Q$replace_this\E}{$with_this}g; $string; }
    For your second:
    sub replace_vars { my ($string, $hash) = @_; $string =~ s{\$(\w+)}{$$hash{$1}}ge; $string; } my %vars = ( a => 1, b => 22, c => 333 ); print replace_vars('a: $a, b: $b, c: $c', \%vars);
      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.
        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: perlquestion [id://702515]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-25 09:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found