Thread Arrays are not lists (14 answers)
Opened by KurtZ at 2008-02-23 17:30

KurtZ
 2008-02-23 17:30
#106297 #106297
User since
2007-12-13
411 Artikel
BenutzerIn
[default_avatar]
Hi

Eine typische Falle sind die subtilen Unterschiede zwischen Lists und Arrays.

Jetzt hab ich mir die Frage gestellt, wie man den Arrayslice in -> Arrays are not lists am effizientesten als Array zurückgeben kann, wenn man den scalaren Kontext bedienen möchte.

Kennt jemand kürzere Lösungen für ret_slice_arr() ??

Code (perl): (dl )
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
use strict;

our $\="\n";

my @A=qw< a b c>;


sub ret_array {
    return @_;
}

sub ret_list {
    return @_[0..$#_];
}

sub ret_slice_arr1 {
        return @{[@_[0..$#_]]};
}

sub ret_slice_arr2 {
        return my @a= @_[0..$#_] ;
}


print scalar(ret_array(@A));                    #> 3    
print scalar(ret_list(@A));                             #> 0
print scalar(ret_slice_arr1(@A));               #> 3
print scalar(ret_slice_arr2(@A));               #> 3
TMTOWTDYOG (there's more than one way to dig your own grave)

View full thread Arrays are not lists