What does the pre-increment of the $ # array mean?

I came across the following line of code. This one has problems:

  • he must do the same thing as press
  • he had to use push
  • hard to read, understand
  • Since then i changed it to use push
  • he does what I considered illegal, but clearly not

here it is:

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

My question is: what does the pre-increment of the $ # array mean? I always considered the $ # array as an attribute of the array and did not write.

+5
source share
5 answers

perldata says:

" - . @days, $# days, csh. , , , 0- . $# days . . , , , ".

$# , .

+14

, , $#array, .

+4

- , .

post-increment, , , . . .

, . , , push. :

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";

:

pre keys: 3
pre: 2 3
post keys: 3
post: 1 2 3
+4

:

$ perl -le 'my @a; $a[@a]="Hello"; $a[@a]=" world!"; print @a'
Hello world!

Perl .

+3

, .

, , . , ++$#array " ", . ( , - , .) , , , : $#array (); . , . :

my @array = qw/1 2 3/;

++$#array;
$array[$#array] = qw/4/;

print "@array\n"

And even, for added pleasure, these are:

my @array = qw/1 2 3/;

$#array += 5;

foreach my $wtf (@array) {
  if (defined $wtf) {
    print "$wtf\n";
  }
  else {
    print "undef\n";
  }
}

And, yes, Cookbook Perl is happy to mess with $#arrayto grow or truncate arrays (chapter 4, recipe 3). I still find it ugly, but maybe it's just a lingering β€œbut that number” of prejudice.

+1
source

All Articles