Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Linked lists as arrays: inserting values

by holli (Monsignor)
on Sep 25, 2006 at 15:54 UTC ( #574767=note: print w/ replies, xml ) Need Help??


in reply to Linked lists as arrays: inserting values

I was pretty sure there was something for this in List::MoreUtils, but as it turned out there isn't. So I came up with this:

sub insert_array_elem { my ($ra, $elem, $index) = @_; # insert $elem before $ra->[$index] my $idx = 0; insert_after { $idx++ == $index-1; } $elem => @{$ra}; }
This doesn't work for the edge cases (first and last element), but hey, that's what pop and friends are there for. Maybe you could write an email to the author of List::MoreUtils to provide an insert_at_index function?.


holli, /regexed monk/


Comment on Re: Linked lists as arrays: inserting values
Select or Download Code
Re^2: Linked lists as arrays: inserting values
by radiantmatrix (Parson) on Sep 25, 2006 at 17:13 UTC
      Yup. I benchmarked my solution against the other two and it is way slower. But: It looks best ;-)


      holli, /regexed monk/

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (11)
As of 2014-04-18 13:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (468 votes), past polls