×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Variable interpolation in LH regexp: s/$x/$hash{$x}/

Variable interpolation in LH regexp: s/$x/$hash{$x}/

Variable interpolation in LH regexp: s/$x/$hash{$x}/

(OP)
I have this file, I want to substitute stuff into it and print it out.  So I have this hash of all the stuff I want to substitute.  The stuff I want to zap is the keys and the values are the stuff I want to substitute in for them.  The test file has multiple occurrences of all the keys.

foreach $key (keys %myhash) {
  if (/$key/) {
      s/$key/$myhash{$key}/;
  }
}

But it doesn't change anything.  How can I get it to EVALUATE $key?  I'm a perl newbie so if you have a better way to do this thing I'm open to it.  thank you.  

RE: Variable interpolation in LH regexp: s/$x/$hash{$x}/

That looks ok actually, presume you're wrapping it in code like this to process files from the command line and write to STDOUT?

while(<>){
  foreach $key (keys %myhash) {
    if (/$key/) {
        s/$key/$myhash{$key}/;
    }
  }
  print;
}

Mike
michael.j.lacey@ntlworld.com
Cargill's Corporate Web Site

RE: Variable interpolation in LH regexp: s/$x/$hash{$x}/

1. You should quote escape symbols in search and replacement strings,
2. The if is obsolete because s handles it,
3. s needs the option g (global) to replace multiple instances of $key:

while(<>){
  foreach $key (keys %myhash) {
     s/\Q$key\E/\Q$myhash{$key}\E/g;
  }
  print;
}

Good luck
Thomas

RE: Variable interpolation in LH regexp: s/$x/$hash{$x}/

(OP)
Yep, the \Q did the trick.  Learning PERL lied and said that using a $x would be variable interpolated but Programming PERL had the true skinny.  Thanks VERY MUCH

It actually works without the \E so now I have to go look up in the book and see what that does.  I thought the if was probably unnecessary too.  

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close