[Vienna-pm] scalar @a vs. scalar @a[0..$#a]
Josef
e9427749 at stud4.tuwien.ac.at
Fri Jul 26 02:08:17 PDT 2013
Hallo allerseits!
Perl gibt im Scalar-Kontext (per DWIM Ratespiel) verschiedenes
zurück, wie Anzahl, Ref, ersten oder eben letzten Wert.
perl5.16.1>
@a=qw(a b c d);
say scalar @a; # 4
say scalar @a[0..3]; # d
Ist so fix definiert? Wo? Somit auch in zukünftige Perl so?
Laut perldata:
@days[3,4,5] # same as ($days[3],$days[4],$days[5])
Und ja:
say scalar( ($a[0],$a[1],$a[2],$a[3]) ); # d
Wenn ja würde es sich als Idiom eignen:
(map{...}@_)[0..$#_] # falls Scalar nur letztes Element statt Anzahl
Zumindest hübscher als:
sub {wantarray ? @_ : $_[-1]}->(map {...}@_)
Unter der Annahme das wenn im Scalar-Kontext verwendet, auch nur ein
Element übergeben wird, bleibt die Performancefrage, auf
@a vs. @a[0..$#a] im Listkontext?
Bzw. andersrum:
say scalar qw(a b c d); # d
say scalar( ()=qw(a b c d) ); # 4
ciao,
Josef
More information about the Vienna-pm
mailing list