In a recent thread I asked for a concrete example from once
own practices that demonstrates the necessity of a standard
'length(array)' feature. Since the widely used GNU Awk
supports that feature I suppose there's some standard cases
where it might be useful (while in my application contexts
I didn't find any examples).
I am typically using these array contexts:
1. Filling an indexed array sequentially
1a.) a[++n] = val
1b.) n = split(..., a, ...)
Both naturally providing the number of elements 'n' in 'a'
without overhead.
2. Using associative arrays
a[key] = val
where I just add a simple counter in cases where I need 'n'
a[key] = val ; ++n
Deleting elements here is straightforward done analogous.
Previous discussions have suggested that it might be useful
in contexts of library functions. On a closer look it seems
also not indicate a necessity for that function.
3. Passing arrays into functions
3a.) f(a) The function designer has chosen to not ask for
'n' then the function would initially need to initially once
count the array elements 'for(i in a) n++' vs. 'n = length(a)'
('n' would be a local function parameter in both cases).
3b.) f(a, n) 'n' passed from outside by the means in 1./2,
then no count is necessary.
4. Returning arrays from functions (i.e. filling arrays in f)
n = f(a) While f is filling the array (cases 1./2.)
it would obtain and return 'n' to the caller.
So the only case I see would be 3a., but the library designer
is free to define the interface as in 3b., or to decide to
write the terse loop 'for(i in a) n++' just once and hidden
in the function. In case of a deliberate design 'f(a)' and
if the length(a) function would access an implicit already
available value that case could be a performance gain.
Are there other cases where that feature might be useful?
Janis
You're confusing "useful" with "necessary".
`length(array)` is not necessary, just like `length(string)` and various other constructs are not necessary.
`length(array)` is useful any time you want to populate and manipulate
an array in your code and then elsewhere in your code determine how many elements the array contains as it saves you from having to write a loop
to do so or manually keep track of adds/deletes using separate variables.
Ed.
In a recent thread I asked for a concrete example from once
own practices that demonstrates the necessity of a standard
'length(array)' feature. [...]
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 295 |
Nodes: | 16 (2 / 14) |
Uptime: | 06:27:03 |
Calls: | 6,642 |
Calls today: | 2 |
Files: | 12,190 |
Messages: | 5,325,988 |