• Poll for examples: using length(array) in practice

    From Janis Papanagnou@21:1/5 to All on Fri Mar 5 16:45:30 2021
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ed Morton@21:1/5 to Janis Papanagnou on Fri Mar 5 10:10:32 2021
    On 3/5/2021 9:45 AM, Janis Papanagnou wrote:
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Janis Papanagnou@21:1/5 to Ed Morton on Fri Mar 5 17:36:31 2021
    On 05.03.2021 17:10, Ed Morton wrote:

    You're confusing "useful" with "necessary".

    `length(array)` is not necessary, just like `length(string)` and various other constructs are not necessary.

    If working around 'length(string)' by a lot of other functions I would
    indeed call that function necessary, and that's one reason why I called
    your comparison silly and not helpful for the core of the question.

    `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.

    And that's where I'd liked to hear from practical examples, since the statements like a[++n] can hardly be seen as a special case of keeping
    track given how we have to operate in Awk. If you mean it as commodity
    feature, that's fine, that explains why I haven't needed it yet, since
    the standard Awk idioms were obviously sufficient in that specific case.

    Janis


    Ed.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Janis Papanagnou@21:1/5 to Janis Papanagnou on Tue Mar 23 00:51:03 2021
    On 05.03.2021 16:45, Janis Papanagnou wrote:
    In a recent thread I asked for a concrete example from once
    own practices that demonstrates the necessity of a standard
    'length(array)' feature. [...]

    As another example use case for that feature see the sample code
    in my response to the thread with subject: "subsets of an array".

    Janis

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)