Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Answer: How do I insert, (not overwrite) into a string?

by mphilip1 (Initiate)
on May 26, 2010 at 20:13 UTC ( #841805=categorized answer: print w/replies, xml ) Need Help??

Q&A > strings > How do I insert, (not overwrite) into a string? - Answer contributed by mphilip1

try this sub I wrote:
# sub signature: # insertXintoYatZ( X , Y , Z ) ; sub insertXintoYatZ{ my ( $X , $Y , $Z ) = @_; substr( $Y , $Z , -length($Y) ) = $X ; return $Y; } $s = "The black cat climbed the green tree"; print "s = $s \n"; print 'insert = '.($s = insertXintoYatZ( "tall ", $s , 26 ))."\n"; print " (now, s = '$s' ) \n"; # OUTPUT: # s = The black cat climbed the green tree # insert = 'tall ' # (now, s = 'The black cat climbed the tall green tree' )";

Replies are listed 'Best First'.
Re: Answer: How do I insert, (not overwrite) into a string?
by bluescreen (Friar) on May 27, 2010 at 13:02 UTC
    try this:
    #!/usr/bin/perl use strict; sub insert_x_into_y_at_z { my ( $x, $y, $z) = @_; $y =~s/^(.{$z})/$1$x/; return $y; }
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2016-12-11 03:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (168 votes). Check out past polls.