Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Linked list in Perl

by Arunbear (Parson)
on Sep 28, 2012 at 12:25 UTC ( #996196=note: print w/ replies, xml ) Need Help??


in reply to Linked list in Perl

Just use an array, and splice to add the new element e.g. to add something after position 5:

+% perl -de0 Loading DB routines from perl5db.pl version 1.32 Editor support available. Enter h or `h h' for help, or `man perldebug' for more help. main::(-e:1): 0 DB<1> @ar = 'a' .. 'h' DB<2> x \@ar 0 ARRAY(0x288c320) 0 'a' 1 'b' 2 'c' 3 'd' 4 'e' 5 'f' 6 'g' 7 'h' DB<3> @slice = @ar[6 .. -1] DB<4> splice @ar, 6, @slice, 11, @slice DB<5> x \@ar 0 ARRAY(0x288c320) 0 'a' 1 'b' 2 'c' 3 'd' 4 'e' 5 'f' 6 11 7 'g' 8 'h' DB<6>

Also see How do I handle linked lists?


Comment on Re: Linked list in Perl
Download Code
Re^2: Linked list in Perl
by Anonymous Monk on Sep 28, 2012 at 12:46 UTC

    To insert , use length zero

    perl -deep oading DB routines from perl5db.pl version 1.33 ditor support available. nter h or `h h' for help, or `perldoc perldebug' for more help. ain::(-e:1): ep DB<1> @f=a..f; DB<2> x\@f ARRAY(0xe2b154) 0 'a' 1 'b' 2 'c' 3 'd' 4 'e' 5 'f' DB<3> splice @f, 3,0, P; DB<4> x\@f ARRAY(0xe2b154) 0 'a' 1 'b' 2 'c' 3 'P' 4 'd' 5 'e' 6 'f' DB<5> q

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2015-07-05 05:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (60 votes), past polls