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

This might be a modest improvement:
use List::Util 'first'; sub insert_after_first { my ($arr, $element, $insert) = @_; if (my $first = first {$arr->[$_] eq $element} 0..$#$arr) { splice @$arr, $first+1, 0, $insert; return 1; } }
There's no point in your returning the array; you're modifying it in-place. You just want to indicate success or failure.

Caution: Contents may have been coded under pressure.

Replies are listed 'Best First'.
Re^2: Inserting an element into an array after a certain element
by halley (Prior) on Apr 01, 2005 at 14:35 UTC
    There's no point in your returning the array; you're modifying it in-place. You just want to indicate success or failure.

    Agreed, but return the value of $first+1 to indicate that it was successful, and it may be of use to some caller (undef otherwise).

    --
    [ e d @ h a l l e y . c c ]