in reply to Inserting an element into an array after a certain element
If elegance is more important than effeciency, try this recursive version:
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.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, @_ ) ); }
In Section
Seekers of Perl Wisdom