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


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

If elegance is more important than effeciency, try this recursive version:

sub insert_after_first { return if @_ < 3; my ( $elem, $new, $front ) = splice @_, 0, 3; return ( $front, $new, @_ ) if $front eq $elem; return ( $front, insert_after_first( $elem, $new, @_ ) ); }
Otherwise, I'd say just keep the version that you've got, although I'd change the loop initialization to for (0 .. $#arr), like ikegami shows above.