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

Okay, I gotta throw my hat in the ring here...

sub insert_after_first { my ($ndx, $arr, $elt, $ins) = (0, @_); $ndx > @$arr and return while $arr->[$ndx++] ne $elt; splice @$arr, $ndx, 0, $ins; }

Update: Here's a version that is a little more efficient, removing one boolean comparison from the loop.

sub insert_after_first { my ($ndx, $arr, $elt, $ins) = (0, @_); $arr->[$ndx++] eq $elt and last for 0 .. @$arr; splice @$arr, $ndx, 0, $ins if $ndx <= @$arr; }

--DrWhy

"If God had meant for us to think for ourselves he would have given us brains. Oh, wait..."