Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it mean to pre-increment $#array?

I've come across the following line of code. It has issues:

  • it is intended to do the same as push
  • it ought to have used push
  • it's hard to read, understand
  • I've since changed it to use push
  • it does something I thought was illegal, but clearly isn't

here it is:

$array [++$#array] = 'data';

My question is: what does it mean to pre-increment $#array? I always considered $#array to be an attribute of an array, and not writable.

like image 498
Paul Beckingham Avatar asked Mar 02 '09 14:03

Paul Beckingham


People also ask

How does pre increment work?

The pre increment operator is used to increment the value of some variable before using it in an expression. In the pre increment the value is incremented at first, then used inside the expression. if the expression is a = ++b; and b is holding 5 at first, then a will hold 6.

Which is better pre increment or post increment?

As a result, pre-increment is faster than post-increment because post-increment keeps a copy of the previous value where pre-increment directly adds 1 without copying the previous value.

What is ++ i and i ++ in C?

In C, ++ and -- operators are called increment and decrement operators. They are unary operators needing only one operand. Hence ++ as well as -- operator can appear before or after the operand with same effect. That means both i++ and ++i will be equivalent.

What is the meaning of post increment?

Post-increment definition (computing) An increment operation that takes place after evaluation of the expression that contains the variable being incremented.


2 Answers

perldata says:

"The length of an array is a scalar value. You may find the length of array @days by evaluating $#days , as in csh. However, this isn't the length of the array; it's the subscript of the last element, which is a different value since there is ordinarily a 0th element. Assigning to $#days actually changes the length of the array. Shortening an array this way destroys intervening values. Lengthening an array that was previously shortened does not recover values that were in those elements."

Modifying $#array is useful in some cases, but in this case, clearly push is better.

like image 156
ysth Avatar answered Nov 09 '22 09:11

ysth


A post-increment will return the variable first and then increment it.

If you used post-increment you would be modifing the last element, since its returned first, and then pushing an empty element onto the end. On the second loop you would be modifing that empty value and pushing a new empty one for later. So it wouldn't work like a push at all.

The pre-increment will increment the variable and then return it. That way your example will always being writing to a new, last element of the array and work like push. Example below:

my (@pre, @post);

$pre[$#pre++] = '1';
$pre[$#pre++] = '2';
$pre[$#pre++] = '3';


$post[++$#post] = '1';
$post[++$#post] = '2';
$post[++$#post] = '3';

print "pre keys: ".@pre."\n";
print "pre: @pre\n";
print "post keys: ".@post."\n";
print "post: @post\n";

outputs:

pre keys: 3
pre: 2 3
post keys: 3
post: 1 2 3
like image 31
mleykamp Avatar answered Nov 09 '22 10:11

mleykamp