I know it's possible to declare an array like this:
我知道可以声明这样的数组:
my @array = ( 5 .. 10 );
which is equivalent to:
这相当于:
my @array = ( 5, 6, 7, 8, 9, 10 );
but is there a similar shorthand when the incremental value is greater than one e.g.
但是当增量值大于1时,是否有类似的简写
my @array = ( 5, 10, 15, 20, 25 );
my @array = ( 100, 200, 300, 400, 500 );
3 个解决方案
#1
22
my @array = map 5*$_, 1..5;
and
和
my @array = map 100*$_, 1..5;
#2
4
More generally:
更普遍:
my $start = 5;
my $stop = 25;
my $increment = 5;
my @array = map $start+$increment*$_, 0..($stop-$start)/$increment;
or:
要么:
chomp(my @array = `seq $start $increment $stop`);
(Just kidding.)
(开玩笑。)
#3
2
You can also use Damian Conway's List::Maker.
你也可以使用Damian Conway的List :: Maker。
use List::Maker;
my @list = <0..100 x 5>;
#1
22
my @array = map 5*$_, 1..5;
and
和
my @array = map 100*$_, 1..5;
#2
4
More generally:
更普遍:
my $start = 5;
my $stop = 25;
my $increment = 5;
my @array = map $start+$increment*$_, 0..($stop-$start)/$increment;
or:
要么:
chomp(my @array = `seq $start $increment $stop`);
(Just kidding.)
(开玩笑。)
#3
2
You can also use Damian Conway's List::Maker.
你也可以使用Damian Conway的List :: Maker。
use List::Maker;
my @list = <0..100 x 5>;