perl5
perl5 copied to clipboard
[doc] Steer users trying to find $# documentation
perlvar says
$# $# was a variable that could be used to format printed numbers. After a deprecation cycle, its magic was removed in Perl v5.10.0 and using it now triggers a warning: "$# is no longer supported". This is not the sigil you use in front of an array name to get the last index, like $#array. That's still how you get the last index of an array in Perl **(see perldata?)**. The two have nothing to do with each other.
Please add the starred stuff I inserted. I am just guessing what should be added.
That twigil syntax could indeed be very confusing, I think.
On Fri, Dec 01, 2023 at 04:01:58PM -0800, Elvin Aslanov wrote:
To be honest I never understood the need for a separate @.***
notation when there is
$array[-1]`.What's the difference, if any, and is it really mentioned somewhere?
I'm not sure what you're referring to here.
This ticket concerns the second of these two:
@a = qw(a b c d);
$size = @a;
$last = $#a;
print "size=$size last=$last\n"; # prints "size=4 last=3"
$#a is usually one smaller than @.***) (it might have been different when we allowed perl to start array indexes at other than 0). Other than that, the main difference is that $#a is assignable to, which can be used to efficiently pre-size or shrink an array:
my @a;
$#a = 999; # pre-allocate slots for 1000 elements
push @a, ... for 0..999;
-- O Unicef Clearasil! Gibberish and Drivel! -- "Bored of the Rings"
On Fri, Dec 01, 2023 at 04:01:58PM -0800, Elvin Aslanov wrote:
To be honest I never understood the need for a separate @.***
notation when there is
$array[-1]`.What's the difference, if any, and is it really mentioned somewhere?
I'm not sure what you're referring to here.
This ticket concerns the second of these two:
@a = qw(a b c d); $size = @a; $last = $#a; print "size=$size last=$last\n"; # prints "size=4 last=3"
$#a is usually one smaller than @.***) (it might have been different when we allowed perl to start array indexes at other than 0). Other than that, the main difference is that $#a is assignable to, which can be used to efficiently pre-size or shrink an array:
my @a; $#a = 999; # pre-allocate slots for 1000 elements push @a, ... for 0..999;
-- O Unicef Clearasil! Gibberish and Drivel! -- "Bored of the Rings"
Yes I got confused, sorry. Deleted my comment in GitHub, but seems like they remain in emails 😊.