Arrays: Difference between revisions
→Some further exposition:: Reduce the hand-waving and outright lies a bit.
Thundergnat (talk | contribs) (→{{header|Perl 6}}: further exposition on arrays in Perl 6) |
Thundergnat (talk | contribs) (→Some further exposition:: Reduce the hand-waving and outright lies a bit.) |
||
Line 3,956:
Arrays are unconstrained by default. They may hold any number of any type of object up to available memory. They do not need to be pre-allocated. Simply assigning (or even referring in some cases) to an index slot is enough to autovivify the container and allocate enough memory to hold the assigned object. Memory will automatically be allocated and will grow and shrink as necessary to hold the values assigned.
Values may be pushed onto the end of an array, popped off of the end, shifted off of the front or unshifted onto the front, and spliced into or out of the interior.
@array.push: 'value';
my $value = @array.pop;
@array.unshift: 'value';
my $value = @array.shift;
@array.splice(2,3, <some arbitrary string values>);
Arrays may be constrained to only accept a certain number of objects or only a certain type of object.
Line 3,989 ⟶ 3,990:
@array[1;1] # same thing, implies rectangular (non-ragged) arrays
There are several
'''List''': A fixed Iterable collection of immutable values. Lists are constructed similarly to arrays:
(1, 2, 3, 4)
('a', 'b', 'c', 'd')
Line 4,011 ⟶ 4,012:
1,1,{$^n2 + $^n1 + 1} ... * # infinite Leonardo numbers
Postcircumfix indexing works for any object that has a Positional (or PositionalBindFailover) role, it need not be in a @-sigiled variable, or indeed, in a variable at all.
[2,4,6,8,10][1] # 4 - anonymous array
<my dog has fleas>[*-2] # 'has' - anonymous list
|