Re:Microfeatures I'd like to see in more languages
by david2ndaccount on Thursday January 05, 2023 @11:05PM
(#34270833)
`$` refers to the length of the array, so `d[$]` is an array-bounds error. `d[$-1]` is needed for the last element, but you can’t do that blindly, you have to check that the length is nonzero or you’ll get unsigned underflow to ulong.max
[ Parent
| on HN ]
Re:Microfeatures I'd like to see in more languages
by WalterBright on Thursday January 05, 2023 @11:47PM
(#34271083)
Which will also cause an array-bounds error. So you're covered!
Re:Microfeatures I'd like to see in more languages
by melagonster on Friday January 06, 2023 @03:02AM
(#34272170)
perl have $d[$#d] represent the index of last element.
Re:Microfeatures I'd like to see in more languages
by bvrmn on Friday January 06, 2023 @06:27AM
(#34273551)
$my_properly_named_array[$#my_properly_named_array] doesn't look like QoL.
Re:Microfeatures I'd like to see in more languages
by tmtvl on Friday January 06, 2023 @07:04AM
(#34273785)
Yeah, but $#list is better used for writing loops:
foreach my $i (0..$#list) {
say "$i: $list[$i]";
}
For getting the last element from a list you can just use -1 (and of course further negative numbers work like you would expect, -2 is second to last and so on): my @last_three = @items[-1, -2, -3];
Re:Microfeatures I'd like to see in more languages
by melagonster on Friday January 06, 2023 @08:36AM
(#34274396)
yes, I forgot perl have negative index!