Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
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?
[Discipulus]: Corion are you would able to realize such thing? O_O
[Corion]: In the same vein I have a script that automates Firefox to enter some data into another system. It's not faster than the people using the script if they were to do it manually, but they prefer not having to check the data and not having typos when ...
[Corion]: ... entering the data
[Corion]: Discipulus: I don't know whether I could really do that, but the init process itself mostly launches other processes, and the whole startup is just following a path of dependencies and making sure they are all running. Which basically is what ...
[Discipulus]: when at work my time is (temporarly) owned by the firm, so i do not care (coworkers whatch movies.. I code Perl)
[Corion]: ... make already does, except for files instead of programs. But maybe with some /proc hackery, that could be eliminated and one could use plain make :-D
[choroba]: systemd just makes is asynchronous
[choroba]: so, make -j
[Corion]: Discipulus: Yeah - but when writing Perl to save time (instead of having fun), it helps to look whether you're actually saving time ;) Why spend 5 minutes doing manually what you can spend three years automating? ;)
[Corion]: choroba: Oh, yeah :-D

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2017-07-27 09:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I came, I saw, I ...
























    Results (408 votes). Check out past polls.