http://qs321.pair.com?node_id=29236

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question: (strings)

eg, insert "H" at the begining of a string, without loosing the 0th element:
"ello" -> "Hello"

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I insert, (not overwrite) into a string?
by KM (Priest) on Aug 23, 2000 at 20:06 UTC
    Another way to do it:

    my $str = "ello"; substr($str,0,0,"H");

    Cheers,
    KM

Re: How do I insert, (not overwrite) into a string?
by Shendal (Hermit) on Aug 23, 2000 at 20:01 UTC
Re: How do I insert, (not overwrite) into a string?
by KM (Priest) on Aug 23, 2000 at 20:21 UTC
    Did a quick benchmark out of curiosity:

    KM:     substr($_,0,0,"H")
    Ovid:    s/^(.*)$/H$1/
    Shendal:  $_ .= "H".$_
    turnstep: s/^/H/

    Benchmark: timing 100000 iterations of KM, Ovid, Shendal, turnstep... KM: -1 wallclock secs ( 0.55 usr + 0.00 sys = 0.55 CPU) Ovid: 2 wallclock secs ( 2.20 usr + 0.00 sys = 2.20 CPU) Shendal: 0 wallclock secs ( 0.53 usr + 0.00 sys = 0.53 CPU) turnstep: 1 wallclock secs ( 0.98 usr + 0.00 sys = 0.98 CPU)

    Cheers,
    KM

Re: How do I insert, (not overwrite) into a string?
by turnstep (Parson) on Aug 23, 2000 at 20:08 UTC
    Regular expressions can handle this just fine as well:
    my $string = "ello"; $string =~ s/^/Henry said H/;
Re: How do I insert, (not overwrite) into a string?
by Ovid (Cardinal) on Aug 23, 2000 at 20:10 UTC
    There are several methods to do this. Shendal has listed one. Here are a couple of others:
    # Concatenation my $str = 'ello'; $str = 'H' . $str; # Regex my $str = 'ello'; $str =~ s/^(.*)$/H$1/;
    Cheers,
    Ovid
Re: How do I insert, (not overwrite) into a string?
by davorg (Chancellor) on Aug 23, 2000 at 20:08 UTC
    my $str = 'ello'; my $char = 'H'; print $str, "\n"; substr($str, 0, 0, $char); print $str, "\n";
Re: How do I insert, (not overwrite) into a string?
by redcloud (Parson) on Aug 24, 2000 at 22:32 UTC
    Just to follow-up KM and davorg, note that you can use substr() as an lvalue. It's just syntactic sugar, but I like the taste of it. 8^)
    use strict; my $str = "ello"; substr($str,0,0) = "H"; print $str, "\n";
Re: How do I insert, (not overwrite) into a string?
by mphilip1 (Initiate) on May 26, 2010 at 20:13 UTC
    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' )";
      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; }
Re: How do I insert, (not overwrite) into a string?
by Fian (Novice) on Apr 02, 2001 at 20:27 UTC
    I know this is kinda long winded but if u wanna insert into a string split the thing into its constituent characters and bung em in an array then u can insert whatever u want wherever u want.....

    @bitsOfString = split(//,$yourString);

    Slán

    Fian. Edited by davido: Added code tags.