Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
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?

Comment on change all occurances of string1 into string2
Download Code
Re: change all occurances of string1 into string2
by toolic (Chancellor) 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 (Abbot) 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 zwon (Monsignor) 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

Re: change all occurances of string1 into string2
by roboticus (Canon) 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.

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?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (9)
As of 2014-12-20 00:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (94 votes), past polls