Update: Oo! In fact, you don't do anything with $new, except stick it back on the front of @_, so don't take it off:sub insert_after_first { return if @_ < 3; my ( $elem, $new, $front ) = splice @_, 0, 3; return ( $front, $front eq $elem ? ($new, @_ ) : insert_after_first( $elem, $new, @_ ) ); }
Then, to change Lispish elegance to Perlish elegance (or madness), make your argument list the way you want it for the recursion, and use & without parentheses to recurse:sub insert_after_first { return if @_ < 3; my ( $elem, $front ) = (splice(@_, 0, 1), splice(@_, 1, 1)); return ( $front, $front eq $elem ? @_ : insert_after_first( $elem, @_ ) ); }
sub insert_after_first { return if @_ < 3; my $front = splice(@_, 2, 1); return ( $front, $front eq $_[0] ? @_[1..$#_] : &insert_after_first ); }
In reply to Re^2: Inserting an element into an array after a certain element
by Roy Johnson
in thread Inserting an element into an array after a certain element
by saintmike
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |