Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

change all occurances of string1 into string2

by Mr. Newb (Acolyte)
on Nov 29, 2012 at 15:16 UTC ( #1006270=perlquestion: print w/replies, xml ) Need Help??
Mr. Newb has asked for the wisdom of the Perl Monks concerning the following question:

So I have a hash, %translate, with key $string1 and value $string2 (there are, of course, multiple of these key/value pairs). I need to look at a line and translate all occurences of $string1 into $string2, so i did:
foreach my $thing (keys(%translate)) { s/${thing}/${translate}{${thing}}/g; }
And of course, %translate is already declared and filled. However, I get the error: Global symbol "$translate" requires explicit package name. Help?

Replies are listed 'Best First'.
Re: change all occurances of string1 into string2
by toolic (Bishop) on Nov 29, 2012 at 15:22 UTC
    Change:
    s/${thing}/${translate}{${thing}}/g;

    to:

    s/${thing}/$translate{${thing}}/g;
      Thanks!
        Thanks!
Re: change all occurances of string1 into string2
by choroba (Chancellor) on Nov 29, 2012 at 15:29 UTC
    Too many curlies. toolic's advice is good, but you can safely remove even more:
    s/$thing/$translate{$thing}/g;
    Be careful, though, if one translation results in a word that another translation might translate again.
    Sometimes, avoiding the inner loop by constructing the regex leads to a faster solution:
    my $re = join '|', keys %translate; $re = qr/($re)/; while (<>) { s/$re/$translate{$1}/g; print; }
    You might need to add some \b boundaries to the regex so it does not match in the middle of a word.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      my $re = join '|', map quotemeta, keys %translate;
      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: change all occurances of string1 into string2
by roboticus (Chancellor) on Nov 29, 2012 at 18:16 UTC

    Mr. Newb:

    There's a bit of a problem in the code. It may or may not affect you though:

    #!/usr/bin/perl use strict; use warnings; my $orig="she sells sea shells down by the seashore\n"; my ($new1, $new2) = ($orig, $orig); my %translate=(she=>'shore', shore=>'sell', sell=>'she'); $new1 =~ s/$_/$translate{$_}/g for keys %translate; print $new1; #print join(", ", keys %translate), "\n"; my $FOO='0000'; $translate{$_}=9999-$_ for 1..10; $new2 =~ s/$_/$translate{$_}/g for keys %translate; print $new2; #print join(", ", keys %translate), "\n"; $ perl x.pl she shes sea shells down by the seashe shore shores sea shorells down by the seashore

    Uncomment the two print statements to see why...

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: change all occurances of string1 into string2
by zwon (Abbot) on Nov 29, 2012 at 15:37 UTC
    And of course, %translate is already declared and filled.

    Where and how? It is not clear from your example and it is the key to the answer.ignore it, toolic and choroba gave the correct answer

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1006270]
Approved by toolic
help
Chatterbox?
[Lady_Aleena]: Rigth now I'm ranting in my head about an old subject.
[1nickt]: Hm, that is, how to prevent Perl from turning 1.0 into 1? I feel sure this must be an faq, but am reading perlnum and not finding the answer ...
[Lady_Aleena]: 1nickt, why does it matter in this case?
[1nickt]: Hm, perhaps this? "Operators which expect an integer force the argument into the integer format."
[1nickt]: Can this be? So print
[1nickt]: ... "expects an integer" ?

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (12)
As of 2017-05-24 18:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?