Problems? Is your data what you think it is? PerlMonks

### Efficient way to replace a set of values with another set of values

by darklord_999 (Acolyte)
 on Nov 23, 2012 at 10:05 UTC Need Help??
darklord_999 has asked for the wisdom of the Perl Monks concerning the following question:

I have a file with values like below

```1. This is just a sample.
2. This is to check
3. How a set of values
4. can be replaced by another
5. set of values and that too
6. in the most efficient way.

I was to replace the 1,2,3,4,5,6 values to a, b ,c, d,e ,f. The final file should look like below:

```a. This is just a sample.
b. This is to check
c. How a set of values
d. can be replaced by another
e. set of values and that too
f. in the most efficient way.
What would be the faster or the most efficient way to do this ?

Replies are listed 'Best First'.
Re: Efficient way to replace a set of values with another set of values
by Cody Fendant (Friar) on Nov 23, 2012 at 10:39 UTC

```
\$str = q{
1. This is just a sample.
2. This is to check
3. How a set of values
4. can be replaced by another
5. set of values and that too
6. in the most efficient way.
};

\$str =~ s/^(\d)/chr(\$1+96)/meg;

print \$str;

### prints:
a. This is just a sample.
b. This is to check
c. How a set of values
d. can be replaced by another
e. set of values and that too
f. in the most efficient way.
Command line version:
```perl -pi -e 's/^(\d)/chr(\$1+96)/e' myfile
Parent beat me to the one line solution, but perhaps a minor mod may be needed; I suspect that the OP may have numbers larger than just 1-6, so perhaps:
```perl -pi -e 's/^(\d+)/chr(\$1+96)/e' myfile
A Monk aims to give answers to those who have none, and to learn from those who know more.
Re: Efficient way to replace a set of values with another set of values
by tobyink (Abbot) on Nov 23, 2012 at 10:12 UTC

Maybe a little something like this?

```my \$string = q{
1. This is just a sample.
2. This is to check
3. How a set of values
4. can be replaced by another
5. set of values and that too
6. in the most efficient way.
};
my %replacements = (
'1.' => 'a.',
'2.' => 'b.',
'3.' => 'c.',
'4.' => 'd.',
'5.' => 'e.',
'6.' => 'f.',
);

my \$regexp = join '|', map quotemeta, keys %replacements;
\$string =~ s/(\$regexp)/\$replacements{\$1}/g;

print \$string;
perl -E'sub Monkey::do{say\$_,for@_,do{(\$monkey=[caller(0)]->[3])=~s{::}{ }and\$monkey}}"Monkey say"->Monkey::do'
Nice solution. In this case, all cases are at the beginning of the line. It might be a good idea to add an anchor in order reduce the likelihood of false positive matches:
```   \$string =~ s/^(\$regexp)/\$replacements{\$1}/gm;
(The m switch had to added so that ^ would match on each line).

When's the last time you used duct tape on a duct? --Larry Wall

Indeed. I was in two minds about whether to include it or not. The substitutions in the examples were consistently at the start of lines, but the OP never explicitly specified that that would always be the case, so I decided to go for the more general "anywhere in the string" match.

perl -E'sub Monkey::do{say\$_,for@_,do{(\$monkey=[caller(0)]->[3])=~s{::}{ }and\$monkey}}"Monkey say"->Monkey::do'
Re: Efficient way to replace a set of values with another set of values
by roboticus (Chancellor) on Nov 23, 2012 at 15:00 UTC

Just curious, but why are you looking for faster / most efficient? It seems that it would be hard to write a method to do the task that would be slow enough to make it worth speeding up --- unless you work at it.

...roboticus

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

Create A New User
Node Status?
node history
Node Type: perlquestion [id://1005242]
Approved by tobyink
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2017-05-29 13:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My favorite model of computation is ...

Results (192 votes). Check out past polls.