Goldberry
Goldberry

Reputation: 65

perl; what is this? for(1..$scalar_name)

I'm reading a perl code to distill what it's doing, but can't figure out what 1..$scalar_name in these lines is doing

my $scalar_name = scalar @array_name;
push @zeroes, 0 for(1..$scalar_name);

Thank you!

Upvotes: 2

Views: 84

Answers (1)

ceving
ceving

Reputation: 23856

Two dots .. is a range operator.

Binary ".." is the range operator, which is really two different operators depending on the context. In list context, it returns a list of values counting (up by ones) from the left value to the right value. If the left value is greater than the right value then it returns the empty list. The range operator is useful for writing foreach (1..10) loops and for doing slice operations on arrays.

Your code takes the number of elements of the array and creates a new array with the same number of zeros.

Upvotes: 4

Related Questions