Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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

( #841805=categorized answer: print w/ replies, xml ) Need Help??

Q&A > strings > How do I insert, (not overwrite) into a string? 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' )";

Comment on Answer: How do I insert, (not overwrite) into a string?
Download Code
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 the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2014-10-25 06:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (142 votes), past polls