str_replace() - String Replace without RegExp
In perl one would replace string without putting much thought into it. You use use the code '$str =~ s/replace_this/with_this/g;' - this is a very efficient and easy way to replace a string. However this statement uses regular expression - which is much more processor intensive than a simple string replace. In many situations I have faced, I had to use a regular expression replace where a string replace would be better. That is because perl don't have a function to do a string replace.
So I have decided to make one for myself...
Code
#Replace a string without using RegExp.
sub str_replace {
my $replace_this = shift;
my $with_this = shift;
my $string = shift;
my $length = length($string);
my $target = length($replace_this);
for(my $i=0; $i<$length - $target + 1; $i++) {
if(substr($string,$i,$target) eq $replace_this) {
$string = substr($string,0,$i) . $with_this . substr($string,$i+$target);
return $string; #Comment this if you what a global replace
}
}
return $string;
}
Useage
string str_replace ( string search, string replace, string subject );
The function uses the same format as the str_replace() function of PHP. The first argument is the search string that is to be replaced, the second argument is the string that it must be replaced with and the third argument is the string in which the replacement must be done. The result will be returned.
Example
$str_regreplace = "Hello World";
$string = "Hello World";
print $string . "\n\n";
$str_regreplace =~ s/Hello/Goodbye/; #Prints 'Goodbye World'
print $str_regreplace . "\n";
print str_replace('Hello','Goodbye',$string); #Prints 'Goodbye World'
Is there a better way to do this?

Comments
I applaud you for trying, but that was one of the worst Perl programs I have ever seen. The general coding of *everything* is just horrible, for instance you are looping through the string letter by letter and checking if the following X letters match what you are looking for, when there is an index() function for that. You should just delete what you have written and upload the following text written by me:
[Commented edited to preserve code formatting]For completeness sake, same code, formatted for pro's instead:
[Commented edited to preserve code formatting]str_replace_1 => the old, regexp method
str_replace_2 => the one recommended by the author
str_replace_3 => the one submitted by anonymous above
and this is what I get:
Benchmark: timing 1000000 iterations of str_replace_1, str_replace_2, str_replace_3...
str_replace_1: 9 wallclock secs ( 9.64 usr + 0.00 sys = 9.64 CPU) @ 103734.44/s (n=1000000)
str_replace_2: 7 wallclock secs ( 5.88 usr + 0.00 sys = 5.88 CPU) @ 170212.77/s (n=1000000)
str_replace_3: 13 wallclock secs (12.45 usr + 0.00 sys = 12.45 CPU) @ 80301.94/s (n=1000000)
If you want to check out the code, well, here they are:
All 3 functions has its own advantages...
s///g; - Perl's native method - requires the least effort.
My Method - Arguably the fastest based on the benchmark provided.
The function provided in the comment - This function basically re-writes my function using better code. I have no idea why it is the slowest.
However, I think there's no reason to write "that was one of the worst Perl programs I have ever seen"
be humble, my son... be humble.
Benchmark: timing 10000 iterations of str_replace_1, str_replace_2, str_replace_
3...
str_replace_1: 0 wallclock secs ( 0.06 usr + 0.00 sys = 0.06 CPU) @ 161290.32
/s (n=10000)
(warning: too few iterations for a reliable count)
str_replace_2: 12 wallclock secs (11.66 usr + 0.02 sys = 11.67 CPU) @ 856.68/s
(n=10000)
str_replace_3: 0 wallclock secs ( 0.13 usr + 0.00 sys = 0.13 CPU) @ 80000.00/
s (n=10000)
(warning: too few iterations for a reliable count)
So yes, it depends on the data you have and the second option is by far the worst for my example, that happened to be a ong string with few occurances to replace.
Then another idiot tries to benchmark and selects a very small string that STARTS with the match target!!!
On any regular test you will see the OP code being horrendously slow. The anonymous poster's "refined" code is a lot faster but of course not near as fast as s///.
Benchmark: timing 1000000 iterations of str_replace_1, str_replace_2, str_replace_3...
str_replace_1: 1 wallclock secs ( 1.96 usr + 0.02 sys = 1.98 CPU) @ 505050.51/s (n=1000000)
str_replace_2: 26 wallclock secs (24.74 usr + 0.09 sys = 24.83 CPU) @ 40273.86/s (n=1000000)
str_replace_3: 4 wallclock secs ( 3.05 usr + 0.01 sys = 3.06 CPU) @ 326797.39/s (n=1000000)
$idx = index( $_, $search, 0);
substr($_, $idx, length($search) ) = $replace;
I need to replace "|P|2.x" with "T|P|2.x" - Once.
That worked for me.
Thanks!
(note, the prize is the warm feeling inside :-)
a, strong, em, b, i, code, pre, pandbrallowed. Other tags will be shown as code(< will become <). Urls, Line breaks will be auto-formated.