View Issue Details

IDProjectCategoryView StatusLast Update
0001566Issue 8 draftsShell and Utilitiespublic2024-06-11 09:12
Reportervapier Assigned To 
PrioritynormalSeverityCommentTypeEnhancement Request
Status ClosedResolutionAccepted As Marked 
Product VersionDraft 2.1 
NameMike Frysinger
Organization
User Reference
Sectionawk
Page Number2447
Line Number79677-79678
Final Accepted Text0001566:0005810
Summary0001566: awk length() should support arrays
DescriptionWhen 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)
Desired ActionUpdate 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.
Tagsissue8

Activities

shware_systems

2022-03-09 19:48

reporter   bugnote:0005735

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.

vapier

2022-03-09 21:57

reporter   bugnote:0005736

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.

nick

2022-03-10 02:23

manager   bugnote:0005737

Re 0001566: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!

geoffclare

2022-04-21 14:35

manager   bugnote:0005810

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.

Issue History

Date Modified Username Field Change
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 => 0001566: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
2024-06-11 09:12 agadmin Status Applied => Closed