Jes*_*pin 6 arrays perl loops constants
我想做同样的事情
my @nucleotides = ('A', 'C', 'G', 'T');
foreach (@nucleotides) {
print $_;
}
Run Code Online (Sandbox Code Playgroud)
但使用
use constant NUCLEOTIDES => ['A', 'C', 'G', 'T'];
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点 ?
zgp*_*max 17
use constant NUCLEOTIDES => [ qw{ A C G T } ];
foreach (@{+NUCLEOTIDES}) {
print;
}
Run Code Online (Sandbox Code Playgroud)
虽然要注意:尽管NUCLEOTIDES是常量,但NUCLEOTIDES->[0]仍可以修改引用数组(例如)的元素.
为什么不让你的常量返回列表?
sub NUCLEOTIDES () {qw(A C G T)}
print for NUCLEOTIDES;
Run Code Online (Sandbox Code Playgroud)
甚至列表上下文中的列表和标量上下文中的数组引用:
sub NUCLEOTIDES () {wantarray ? qw(A C G T) : [qw(A C G T)]}
print for NUCLEOTIDES;
print NUCLEOTIDES->[2];
Run Code Online (Sandbox Code Playgroud)
如果您还需要经常访问单个元素.