Cynical news

Links
Literature
Music
Programming
Software
Programming
Perl Arrays
Perl Arrays
Initialise

Fixed contents n/a @a=(1,2,3,4,5);
1 2 3 4 5

Insert

Prepend element 'e' @a=(2,3,4);
$e=1;
unshift @a, $e;
1 2 3 4
Prepend element 'e' @a=(2,3,4);
$e=1;
splice @a, 0, 0, $e;
1 2 3 4
Append element 'e' @a=(1,2,3);
$e=4;
push @a, $e;
1 2 3 4
Insert element 'e' at index 'n' @a=(1,2,3,4,5);
$e=666;
$n=3;
splice @a, $n, 0, $e;
1 2 3 666 4 5
Prepend array 'b' @a=(1,2,3,4,5);
@b=(10,20,30);
splice @a, 0, 0, @b;
10 20 30 1 2 3 4 5
Append array 'b' @a=(1,2,3,4,5);
@b=(10,20,30);
push @a, @b;
1 2 3 4 5 10 20 30
Insert array 'b' at index 'n' @a=(1,2,3,4,5);
@b=(10,20,30);
$n=3;
splice @a, $n, 0, @b;
1 2 3 10 20 30 4 5
Concatenate array 'a' and array 'b' @a=(1,2,3);
@b=(4,5,6);
@c=(@a,@b);
1 2 3 4 5 6
Insert element 'e' at indices in 'b' @a=(1,2,3,4,5);
@b=(0,2,4);
$e=666;
foreach $n(reverse(@b)) { splice @a, $n, 0, $e; }
666 1 2 666 3 4 666 5

Overwrite

Overwrite first element @a=(1,2,3);
$a[0]=666;
666 2 3
Overwrite last element @a=(1,2,3);
$a[-1]=666;
1 2 666
Overwrite element at index 'n' @a=(1,2,3);
$n=1;
$a[$n]=666;
1 666 3
Overwrite elements from index 'n' (inclusive) @a=(1,2,3,4,5,6);
@b=(7,8,9);
$n=2;
splice @a, $n, scalar @b, @b;
1 2 7 8 9 6

Count

Length of array @a=(1,2,3);
$n=scalar @a;
3

Retrieve by index

Retrieve first element @a=(1,2,3);
$e=$a[0];
1
Retrieve last element @a=(1,2,3);
$e=$a[-1];
3
Retrieve element 'n' @a=(1,2,3);
$n=2;
$e=$a[$n];
3

Remove by index

Remove element at index 'n' @a=(1,2,3,4,5);
$n=2;
splice @a, $n, 1;
1 2 4 5
Remove elements in index range 'm' to 'n' (inclusive) @a=(1,2,3,4,5);
$m=1;
$n=3;
splice @a, $m, $n;
1 5
Remove elements from index 'n' onwards (inclusive) @a=(1,2,3,4,5);
$n=3;
splice @a, $n, $#a-$n+1;
1 2 3

Slice

Slice from index 'm' to 'n' (inclusive) @a=(1,2,3,4,5);
$m=1;
$n=3;
@a=@a[$m..$n];
2 3 4
Slice from index 'n' onwards (inclusive) @a=(1,2,3,4,5);
$n=2;
@a=@a[$n..$#a];
3 4 5
Slice up to index 'n' (inclusive) @a=(1,2,3,4,5);
$n=3;
@a=@a[0..$n];
1 2 3 4
Slice non-contiguous indices 'x','y' and 'z' @a=(1,2,3,4,5);
$x=0; $y=2; $z=4;
@a=@a[$x,$y,$z];
1 3 5

Search

Test element 'e' 'is in' array @a=(1,2,3,4,5);
$e=3;
$t=grep {$_ eq $e } @a;
1

Sort

Sort in ascending order @a=(5,1,4,3,2);
@a=sort {$a <=> $b} @a;
1 2 3 4 5
Sort in descending order @a=(5,1,4,3,2);
@a=sort {$b <=> $a} @a;
5 4 3 2 1
Reverse @a=(1,2,3,4,5);
@a=reverse(@a);
5 4 3 2 1

Use as a stack

Push element 'e' @a=(1,2,3,4,5);
$e=666;
push @a, $e;
1 2 3 4 5 666
Pop next element @a=(1,2,3,4,5);
pop @a;
1 2 3 4
Retrieve next element @a=(1,2,3,4,5);
$e=$a[-1];
5

Use as a FIFO

Add element 'e' @a=(1,2,3,4,5);
$e=666;
push @a, $e;
1 2 3 4 5 666
Remove next element @a=(1,2,3,4,5);
shift @a;
2 3 4 5
Retrieve next element @a=(1,2,3,4,5);
$e=$a[0];
1

Compare

Test for equality (true) @a=(1,2,3,4,5);
@b=(1,2,3,4,5);
$t=(sub { return 0 if scalar $a != scalar $b; for (my $i=0; $i<scalar @a; $i++) { return 0 if $a[$i] != $b[$i]; } return 1 })->(\@a,\@b);
1
Test for equality (false) @a=(1,2,3,4,5);
@b=(1,2,3,4,50);
$t=(sub { return 0 if scalar $a != scalar $b; for (my $i=0; $i<scalar @a; $i++) { return 0 if $a[$i] != $b[$i]; } return 1 })->(\@a,\@b);
0

www.cynicalsoftware.com
14-11-09@15:37:24