Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Inserting an element into an array after a certain element

by tlm (Prior)
on Mar 31, 2005 at 21:23 UTC ( [id://443980]=note: print w/replies, xml ) Need Help??


in reply to Inserting an element into an array after a certain element

% perl -le 'print"@{[split//,join q(x),split/(?<=c)/,join(q(),qw(a b c + d c e f)),2]}"' a b c x d c e f
Pretty perverse (perl-verse?).

the lowliest monk

Replies are listed 'Best First'.
Re^2: Inserting an element into an array after a certain element
by CountZero (Bishop) on Apr 01, 2005 at 05:22 UTC
    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

      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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://443980]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-24 23:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found