Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are arrays implemented in Perl?

The Perl array is an abstract data type. What's the internal mechanism for the Perl array? Is it implemented with dynamic array or linked list? Since the array elements have random access, I would assume a dynamic array of pointers, or references to scalars make sense. However, with shift and unshift operation at the head of array, would the array have to move all its elements with these operations? sound inefficient to me. Any thought?

like image 942
Jason X Avatar asked Jun 28 '10 05:06

Jason X


People also ask

How is an array declared in Perl?

Array Creation: In Perl programming every array variable is declared using “@” sign before the variable's name. A single array can also store elements of multiple datatypes.

How are arrays implemented?

Implementation of arrays performs various operations like push (adding element), pop (deleting element) element at the end of the array, getting the element from particular index, inserting and deleting element from particular index.

What is array in Perl?

An array is a variable that stores an ordered list of scalar values. Array variables are preceded by an "at" (@) sign. To refer to a single element of an array, you will use the dollar sign ($) with the variable name followed by the index of the element in square brackets.


1 Answers

Have a look at this: http://www.perlmonks.org/?node_id=17890

(taken from there:)

Perl implements lists with an array and first/last element offsets. The array is allocated larger than needed with the offsets originally pointing in the middle of the array so there is room to grow in both directions (unshifts and pushes/inserts) before a re-allocation of the underlying array is necessary. The consequence of this implementation is that all of perl's primitive list operators (insertion, fetching, determining array size, push, pop, shift, unshift, etc.) perform in O(1) time.

like image 170
phimuemue Avatar answered Sep 28 '22 19:09

phimuemue