in reply to Inserting an element into an array after a certain element
There's no point in your returning the array; you're modifying it in-place. You just want to indicate success or failure.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; } }
|
|---|
| 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 |