in reply to Re: add elements to array at certain positions
in thread add elements to array at certain positions

Thanks. Is there a similar method if the arrays are strings?
  • Comment on Re^2: add elements to array at certain positions

Replies are listed 'Best First'.
Re^3: add elements to array at certain positions
by GrandFather (Saint) on Oct 30, 2008 at 05:43 UTC

    split the string(s) into a list of characters, use the map as shown, then join the result back together.


    Perl reduces RSI - it saves typing
Re^3: add elements to array at certain positions
by ikegami (Patriarch) on Oct 30, 2008 at 08:54 UTC

    The method demonstrated below is devious and should be avoided in favour of something more conventional.

    my $stra = '0157953'; my $strb = 'abcyefzaa'; (my $new = $strb) =~ s/([yz])|./ (defined($1) ? 'I' : ($stra =~ m{(.)}sgc ? $1 : die ) ) /seg; print("$new\n"); # 015I79I53

    Update: Saner: (no nesting)

    my $stra = '0157953'; my $strb = 'abcyefzaa'; my $new = ''; while ($strb =~ /([yz])|./sg) { $new .= (defined($1) ? 'I' : ($stra =~ /(.)/sgc ? $1 : die ) ); } print("$new\n"); # 015I79I53