1
2
3
4
my @arr = ("null","eins","zwei","drei");
func(\@arr); # ok, aber auf "null"
func(\@arr+2); # falsch
func(\@arr[2]; # auch falsch
1 2 3 4 5 6 7 8 9 10
use strict; use warnings; sub func { my $arr = shift; print $$arr; } my @arr = ("null","eins","zwei","drei"); func(\$arr[2]);
1 2 3 4 5 6 7 8
sub func { my $arr_ref = shift; my $index = shift; print $arr_ref->[$index]; } my @arr = ("null","eins","zwei","drei"); func(\@arr,2);
1 2 3 4 5
# 1. foo( \@array, 2 ); # Array-Ref and Start-Index for array # 2. foo( [ @array[ 2 .. $#array ] ); # Array-Ref with data from Array-Slice (copy of data!)
1
2
3
4
5
6
+---------------------------+
@array | null | eins | zwei | drei |
+---------------------------+
^
|
$arr_ref -----------------+
1
2
3
4
5
6
7
+----------+-----------+-----------+------+
*array[] = | adress_1 | address_2 | address_3 | NULL |
+----------+-----------+-----------+------+
*address_1 = "null\0";
*address_2 = "eins\0";
*address_3 = "zwei\0";
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
use 5.012; package ArraySlice; use base qw(Tie::Array); sub new { my ($Self, $base, $offset, $length) = @_; bless { base => $base, offset => $offset, length => $length }, $Self; } sub newref { my ($Self, @args) = @_; tie my @slice, $Self, @args; return \@slice; } sub TIEARRAY { my ($Self, @args) = @_; return $Self->new(@args); } sub EXISTS { my ($self, $index) = @_; return 0 <= $index && $index < $self->{length}; } sub FETCH { my ($self, $index) = @_; if ($self->EXISTS($index)) { return $self->{base}->[$self->{offset} + $index]; } else { return undef; } } sub FETCHSIZE { my ($self) = @_; return $self->{length}; } package main; my @foo = qw(oans zwoa dreie fiare finwe); my $slice = ArraySlice->newref(\@foo, 1, 3); say foreach @$slice;
2015-12-28T16:51:46 murphy.... Der Interpreter erlaubt Dir nicht, eine Referenz auf einen Teil eines Arrays zu erzeugen.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use Data::Alias; sub foo($) { my $array = shift; $array->[0]='3'; $array->[1]='4'; unshift(@$array,'<<<'); push(@$array,'>>>'); print Dumper($array); } my @array = qw(eins zwei drei vier); print Dumper(\@array); foo alias [ @array[2 .. $#array] ] ; print Dumper(\@array);
1 2 3
my @arr = ("null","eins","zwei","drei"); my $ref = \@arr; $ref++; # nicht möglich ist
1 2 3
$ref = getRefByValue(@add,"zwei"); $ref = getRefByOffset(@arr,2); $ref = alterRef($ref,-1);