in reply to Re: Inserting an element into an array after a certain element
in thread Inserting an element into an array after a certain element

This works for the example given, but will break if one of the elements contain white-space.

CountZero

"If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

  • Comment on Re^2: Inserting an element into an array after a certain element

Replies are listed 'Best First'.
Re^3: Inserting an element into an array after a certain element
by tlm (Prior) on Apr 01, 2005 at 10:36 UTC

    Oh, it breaks if any of the elements consists of more than one character:

    % perl -le 'print"@{[split//,join q(x),split/(?<=c)/,join(q(),qw(a b c + d c e foo f)),2]}"' a b c x d c e f o o f
    but single spaces are fine:
    % perl -le 'print"@{[split//,join q(x),split/(?<=c)/,join(q(),qw(a b c + d c e),q( ),q(f)),2]}"' a b c x d c e f
    To fix this, I guess one could do something like
    perl -le 'print"@{[split/\0/,join qq(\0x),split/(?<=c)/,join(qq(0),qw( +a b c d c e foo f)),2]}"' a b c x d c e foo f

    the lowliest monk