Update `push()` documentation to include examples and be more novice friendly #20737
I like the new additions, but I don't like the existing example using a loop. I feel like the entire "A more technical explanation:" to "but is more efficient." section could be eliminated. It seems unneeded and inaccurate.
Shouldn't this be $ARRAY[@ARRAY] = $value;?
Mostly looks ok to me. One minor nit, I don't think we should use $#ARRAY++ in docs for push @ARRAY is more natural, and doesn't require understanding that you can change the size if the array by assigning to $#ARRAY. (Frankly that operation is rare enough that I had forgotten it was legal, presizing strings and hashes is rarely done but is still more common IME than presizing arrays.).
I think the intent is to show (to someone intimately familiar with how $#prefix works) that the operation increases the size of the array and then assigns the element there. But I agree with @haarg that this entire example doesn't give any useful information.
I'm good with removing, but if we don't we should restate as I diid. The original is cryptic, and unnecessary (as you know there is no need to presize the array to store into an index) and we shouldn't explain one concept by invoking a separate and even more complex concept. Assigning a value to the next available index is often done the way I wrote it (ive done it myself many times [although usually in two statements] and ive seen others do it as well), but I can't recall ever having seen it done the way it's shown in that example.
Two open parens, one closing.
There's a few of these, and in the unshift examples. Not sure I'd bother with the inner parens, the subs are variadic and the parens might confuse people? YMMV
Good catch... I updated the PR
It was pretty unanimous that the original technical definition didn't add a lot to the documentation so I removed it. The diff looks gnarly now because of all the text moving around, but the wording is MUCH cleaner now.
What's next on this PR?
I'd like to work on shift() and pop() doc fixes next, but I want to focus on this until it lands.
lgtm
Ping
I don't like the return value example. You effectively say the same thing 3 times ("returns the number of elements") without adding any more information or clarity. Showing the return value being printed out doesn't add anything. Perheps get rid of the 'say' and change the comment line just above the 'push' to
sets $color_count to 5
This ticket was stalled. I have removed the comment that it was stalled by.
And I will merge once CI passes successfully.
Approved these changes.
Thanks. Applied.
demerphq
scottchiefbaker
The
push()documentation is very developer heavy. I added some examples and simplified the wording so it's more novice friendly.