Reputation: 487
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.
my @array = ( 5, 10, 15, 20, 25 );
my @array = ( 100, 200, 300, 400, 500 );
Upvotes: 8
Views: 3353
Reputation: 510
You can also use Damian Conway's List::Maker.
use List::Maker;
my @list = <0..100 x 5>;
Upvotes: 2
Reputation: 98398
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.)
Upvotes: 4