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

Substitution in text files

by Angel (Friar)
on Jun 15, 2002 at 19:23 UTC ( #174865=perlquestion: print w/ replies, xml ) Need Help??
Angel has asked for the wisdom of the Perl Monks concerning the following question:

Ok getting hammered by something that should not be that difficult again. I want to take a hash and substitute the keys with $keyname from a text file and place the hash value in it's place. I tried and tried and I cant get it to do the substitution
#hash that contins the data %recordData = %$hashRef; #gets the email address $emailAddress = $recordData{ "EmailAddress" }; $message = $original_message; local($/) = undef; foreach $key (keys %recordData) { my $subVar = $recordData{$key}; my $subKey = "\$" . $key; # prints the data to the screen ( debugging ) print "$subKey = $subVar\n"; $message =~ s/\$subKey/$subVar/o; print "$message"; }
any clue on how to get this to work?

Comment on Substitution in text files
Download Code
Re: Substitution in text files
by jepri (Parson) on Jun 15, 2002 at 20:14 UTC
    s/\$subKey/$subVar/

    replaces the literal '$subKey' with $subvar. What you might have wanted is

    s/$subKey/$subVar/

    ____________________
    Jeremy
    I didn't believe in evil until I dated it.

Re: Substitution in text files
by Weasel (Sexton) on Jun 15, 2002 at 20:19 UTC
    you need not
    $message =~ s/\$subKey/$subVar/o;
    but rather
    my $qkey = quotemeta $key; $message =~ s/$qkey/$recorsData{$key}/;
    but your code snippet and your question gives me an idea that you're completely lost. Where's reading of a file after local($/)=undef;? (which is easier to write as just local $/;) where's file writing?

    To say, to answer a question you should know 50% of answer...

    Give us more details and your problem will be easier to advice...

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2014-04-21 10:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (493 votes), past polls