Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring an array with incremental values in Perl

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 );
like image 819
Keith Broughton Avatar asked May 22 '12 00:05

Keith Broughton


2 Answers

my @array = map 5*$_, 1..5;

and

my @array = map 100*$_, 1..5;
like image 140
ikegami Avatar answered Sep 24 '22 20:09

ikegami


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.)

like image 25
ysth Avatar answered Sep 21 '22 20:09

ysth