CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Fun With Perl: Perl Golf: Re: [RedRum] String Replacement Golf: Edit Log



fashimpaur
User / Moderator

Feb 15, 2002, 6:07 AM


Views: 48819
Re: [RedRum] String Replacement Golf

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:


Code
 $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)


Edit Log:
Post edited by fashimpaur (User) on Feb 15, 2002, 6:15 AM
Post edited by fashimpaur (User) on Feb 15, 2002, 6:16 AM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives