
fashimpaur
User
/ Moderator
Feb 15, 2002, 6:07 AM
Post #18 of 19
(99584 views)
|
Re: [RedRum] String Replacement Golf
[In reply to]
|
Can't Post
|
|
My last solution was: sub mysubstr{$_[0]=~/(.{$_[1]}).{$_[2]}/;$1.$_[3].$'} the method is: mysubstr(EXPR, OFFSET, LEN, REPLACEMENT); Therefore the code reads: Find in $_[0] a match of $_[1] number of any type of character, followed by $_[2] number of any type of character. (The searching of the string will stop as soon as the first match is found.) Then concatenate the first parenthesized match and the replacement ($_[3]) and anything that followed the whole match and put the result in $_. Since mysubstr is called as: $e = mysubstr($a, $b, $d, $c), the correct result is printed. Did I miss something here? My whole code test is: $a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; # EXPR $b = 15; # OFFSET $c = "Perl_Is_Fun"; # REPLACEMENT $d = length $c; # LEN # the method would be called as # $e = mysubstr($a, $b, $d, $c); # test result # print $e; # (expecting: abcdefghijklmnoPerl_Is_FunABCDEFGHIJKLMNOPQRSTUVWXYZ) # print "\n\n"; print length q!$_[0]=~/(.{$_[1]}).{$_[2]}/;$1.$_[3].$'!; sub mysubstr{$_[0]=~/(.{$_[1]}).{$_[2]}/;$1.$_[3].$'}
Let me know if there is anything that does not conform to the rules. I thought I did it right. Dennis $a="c323745335d3221214b364d545". "a362532582521254c3640504c3729". "2f493759214b3635554c3040606a0", print unpack"u*",pack "h*",$a,"\n\n";
(This post was edited by fashimpaur on Feb 15, 2002, 6:16 AM)
|