Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

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

by mphilip1 (Initiate)
on May 26, 2010 at 20:13 UTC ( [id://841805]=note: print w/replies, xml ) Need Help??


in reply to How do I insert (not overwrite) into a string?

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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://841805]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-19 17:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found