Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Inserting an element into an array after a certain element

by BrowserUk (Patriarch)
on Mar 31, 2005 at 18:56 UTC ( [id://443928]=note: print w/replies, xml ) Need Help??


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

Updated to address Roy Johnson's concerns below

sub insertAfter{ use List::Util qw[ first ]; my( $aref, $before, $insert ) = @_; my $first = first{ $aref->[ $_ ] eq $before } 0 .. $#$aref ; return unless defined $first; splice @{ $aref }, ++$first, 0, $insert; }

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco.
Rule 1 has a caveat! -- Who broke the cabal?

Replies are listed 'Best First'.
Re^2: Inserting an element into an array after a certain element
by Roy Johnson (Monsignor) on Mar 31, 2005 at 19:11 UTC
    If the $before isn't found, though, it inserts $insert after the first element.

    Caution: Contents may have been coded under pressure.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://443928]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-16 04:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found