I want to do the same as below
my @nucleotides = ('A', 'C', 'G', 'T');
foreach (@nucleotides) {
print $_;
}
but using
use constant NUCLEOTIDES => ['A', 'C', 'G', 'T'];
How can I do that ?
I want to do the same as below
my @nucleotides = ('A', 'C', 'G', 'T');
foreach (@nucleotides) {
print $_;
}
but using
use constant NUCLEOTIDES => ['A', 'C', 'G', 'T'];
How can I do that ?
Why not make your constant return a list?
sub NUCLEOTIDES () {qw(A C G T)}
print for NUCLEOTIDES;
or even a list in list context and an array ref in scalar context:
sub NUCLEOTIDES () {wantarray ? qw(A C G T) : [qw(A C G T)]}
print for NUCLEOTIDES;
print NUCLEOTIDES->[2];
if you also need to frequently access individual elements.
use constant NUCLEOTIDES => qw/A C G T/.(This is for completeness, while https://stackoverflow.com/a/8972542/6607497 is more elegant)
After having tried things like @{NUCLEOTIDES} and @{(NUCLEOTIDES)} unsuccessfully, I came up with introducing an unused my variable:
foreach (@{my $r = NUCLEOTIDES}) {
}