Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
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 avoiding work at the Monastery: (14)
As of 2015-07-02 18:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (44 votes), past polls