linked lists in perl?

Peter Scott peter at PSDT.com
Thu Nov 7 18:43:03 CST 2002


At 08:05 PM 11/6/02 -0800, nkuipers wrote:
>Hello everyone,
>
>I recently acquired one of those "Essentials" pocket books; this one 
>is called
>Data Structures I.  The chapter I am on right now deals with linked lists.  I
>admit that the extent of my search for link-list-like structs in Perl 
>has been
>limited to scanning the index of the Camel 3rd (zilch), but does anyone know
>how linked lists could be emulated in Perl, if at all

When I program in C, I have to create linked lists all the 
time.  Everyone raise your hand if you're sick of writing the same kind 
of code all the time for doing that and figuring out whether you've 
made a fencepost error in the code to find an element and insert a new one.

In Perl, I've never needed a linked list.  A linked list is not a 
"natural" structure, it's something computer programmers invented to 
solve certain kinds of problems because the language didn't natively 
support the kinds of data structures the problems called for.

Well, Perl does.  Most of the need for linked lists is for dynamically 
resizable lists.  But Perl's arrays are just that.

Next, there's the benefit of keeping things in sorted order with 
minimal disturbance when inserting a new element.  AFAIK splice() 
results in equally minimal disturbance, although I usually just sort 
the whole thing when I need it and seldom have to worry about performance.

Hmmm... any other reason to use a linked list instead of an 
array?  Now, when you're talking about non-linear structures, e.g. 
trees, things are different; you can just use some anonymous array or 
hash to hold each node and make references to them for pointers.

Not surprising that you didn't think Perl had pointers.  For reasons 
unknown to me, Randal goes out of his way to avoid all mention of 
references in Llama III.  It's a remarkable trick, actually, 
considering that he even discusses objects.  Some time I must ask him 
why he did that.


--
Peter Scott
Pacific Systems Design Technologies
http://www.perldebugged.com/




More information about the Victoria-pm mailing list