|Anonymous | Login||2023-03-29 12:21 UTC|
|Main | My View | View Issues | Change Log | Docs|
|Viewing Issue Simple Details [ Jump to Notes ]||[ Issue History ] [ Print ]|
|ID||Category||Severity||Type||Date Submitted||Last Update|
|0001566||[Issue 8 drafts] Shell and Utilities||Comment||Enhancement Request||2022-03-09 10:02||2022-05-23 11:41|
|Priority||normal||Resolution||Accepted As Marked|
|Status||Applied||Product Version||Draft 2.1|
|Final Accepted Text||Note: 0005810|
|Summary||0001566: awk length() should support arrays|
When working with arrays, getting the number of elements in the array can be helpful. Many awk implementations already support this.
* BWK awk 20020101
length(arrayname) returns number of elements; thanks to
arnold robbins for suggestion.
* gawk 3.1.5
15. length(array) now returns the number of elements in the array. This is
is a non-standard extension that will fail in POSIX mode.
* mawk 1.3.4-20121129
+ support length(array), as done in gawk and BWK awk (Original-Mawk #1).
* BusyBox awk 1.22.0
awk: fix length(array)
* QSE awk (version unknown)
These are known to not support it:
* awka 0.7.5 (2001)
Update the description of the length function:
If the argument is an array, returns the number of elements, otherwise return the length, in characters, of its argument taken as a string, or of the whole record, $0, if there is no argument.
|The current definition says treat the array as a string and apparently count all characters in it, probably faster than a program looping over each element and adding up individual lengths. As such this appears an improper overload of the function name, hiding that functionality. It is better, to me, the province of a separate General function, e.g. arrsize(varname), returning 1 if it isn't an array.|
i'm not aware of any implementation behaving that way, and arguably, would be undesirable surprising behavior. if we look at how this particular function is implemented in other languages (e.g. Python or C++), it's quite normal for the same function name to be context-aware.
considering implementations have already implemented length in the "count-elements-not-bytes", and it's been live for decades in some implementations, i think that's a pretty good signal that (1) the behavior isn't surprising for users and (2) no one wants the functionality you describe.
Re Note: 0005735 : on page 2446 (draft 2.1) the standard clearly states that if an array name is passed as a parameter that the function uses as a scalar (which is the case with length at present) then the behavior is undefined.
I support the addition of this new (to the standard) usage. Defining something that was previously undefined and matches existing practice is always good!
The following is the desired action with some editorial wording changes...
Replace the description of the length function with:
length[([arg])]If arg is an array, return the number of elements in the array; otherwise, return the length, in characters, of arg taken as a string, or of the whole record, $0, if there is no argument.
|2022-03-09 10:02||vapier||New Issue|
|2022-03-09 10:02||vapier||Name||=> Mike Frysinger|
|2022-03-09 10:02||vapier||Section||=> awk|
|2022-03-09 10:02||vapier||Page Number||=> 0|
|2022-03-09 10:02||vapier||Line Number||=> 0|
|2022-03-09 18:58||Don Cragun||Page Number||0 => 2447|
|2022-03-09 18:58||Don Cragun||Line Number||0 => 79675-79676|
|2022-03-09 18:59||Don Cragun||Line Number||79675-79676 => 79677-79678|
|2022-03-09 19:48||shware_systems||Note Added: 0005735|
|2022-03-09 21:57||vapier||Note Added: 0005736|
|2022-03-10 02:23||nick||Note Added: 0005737|
|2022-04-21 14:35||geoffclare||Note Added: 0005810|
|2022-04-21 15:14||geoffclare||Final Accepted Text||=> Note: 0005810|
|2022-04-21 15:14||geoffclare||Status||New => Resolved|
|2022-04-21 15:14||geoffclare||Resolution||Open => Accepted As Marked|
|2022-04-21 15:15||geoffclare||Tag Attached: issue8|
|2022-05-23 11:41||geoffclare||Status||Resolved => Applied|
|Mantis 1.1.6[^] Copyright © 2000 - 2008 Mantis Group|