Creating an Array: Difference between revisions

Content deleted Content added
Added Toka
Line 296: Line 296:
==[[Perl]]==
==[[Perl]]==
[[Category:Perl]]
[[Category:Perl]]
'''Interpeter:''' [[Perl]]
'''Interpreter:''' [[Perl]] 5


my @empty;
use vars qw{ @Array };
my @empty_too = ();


my @populated = ('This', 'That', 'And', 'The', 'Other');
@Array=(
print $populated[2];
[0,0,0,0,0,0],
# And
[1,1,1,1,1,1],
[2,2,2,2,2,2],
my $aref = ['This', 'That', 'And', 'The', 'Other'];
[3,3,3,3,3,3]
print aref->[2];
);
# And
#You would call the array by this code. This will call the 3rd 1 on the second list
print $Array[1][3];


# having to quote like that really sucks, and that's why we got syntactic sugar
# Alternative:
my @array_using_qw = qw/coffee sugar cream/;
my @wakey_wakey = qw(coffee sugar cream);
push @wakey_wakey, 'spoon';
# add spoon to right-hand side
my $cutlery = pop @wakey_wakey;
# remove spoon
unshift @wakey_wakey, 'cup';
# add cup to left-hand side
my $container = shift @wakey_wakey;
# remove cup


my @multi_dimensional = (
# Alternative:
[0, 1, 2, 3],
my @Array3 = ();
[qw(a b c d e f g)],
push @Array3, "Item1";
[qw(! $ % & *)],
push @Array3, "Item2";
);
$Array3[2] = "Item3";
print $multi_dimensional[1][3];
$Array3[3][0] = "Item4";
# d

@Array = ('This', 'That', 'And', 'The', 'Other');
my $mdref = [

$ArrayRef = ['This', 'That', 'And', 'The', 'Other'];
[0, 1, 2, 3],
[qw(a b c d e f g)],
print $ArrayRef->[2]; # would print "And"
[qw(! $ % & *)],
];
print $mdref->[1][3];
# d


==[[PHP]]==
==[[PHP]]==