Leser: 22
1 2 3 4
# Wert vorne anfügen: unshift @store, $value; # Wert hinten rauswerfen, falls Array zu lang: pop @store if (@store > $max_count);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package My::Queue;
use strict;
use warnings;
sub new {
my $class = shift;
my $size = shift || 7;
my $self = {};
$self->{'size'} = $size;
$self->{'queue'} = [ ];
$#{$self->{'queue'}} = $size-1;
bless $self, $class;
return $self;
}
sub put {
my $self = shift;
my @values = @_;
unshift @{$self->{'queue'}},@values;
$#{$self->{'queue'}} = $self->{'size'}-1;
return scalar @values;
}
sub get {
my $self = shift;
my $index = shift;
die ('Queue: Index out of bounds')
if ($index < 0 or $index > $self->{'size'}-1);
return $self->{'queue'}->[$index];
}
package main;
use strict;
use warnings;
my $q = My::Queue->new;
$q->put(1,2,3);
print "Nummer 0: " . $q->get(0) . "\n";
print "Nummer 2: " . $q->get(2) . "\n";
#print "Nummer 3: " . $q->get(3) . "\n"; # wirft ein die!
#print "Nummer -9: " . $q->get(-9) . "\n"; # wirft ein die!
$q->put(99,88,77,66,55);
1;