bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Question on the behavior of length()


From: Ed Morton
Subject: Re: Question on the behavior of length()
Date: Mon, 11 Dec 2023 06:37:49 -0600
User-agent: Mozilla Thunderbird

Just a suggestion regarding calling `length()` for push()/pop() stack operations:

On 12/10/2023 8:18 AM, Christian Schmidt wrote:
Hi all,

First of all I'd like to state that I do not consider the following necessarily a bug; however I'd like to discuss the current implementation, and have not found a better place to do so.

My issue is that I can't use

x[length(x)] = y

e.g. to emulate to push to a stack, without explicitly converting x into an array first, e.g. by "delete x".

No need to call `length(x)` for every push()/pop(), you can implement a stack by storing it's top index in `x[0]` (or some other unused, probably negative, index instead if you prefer):

     function push(x,y) { x[++x[0]] = y }
     function pop(x) { delete x[x[0]--] }

As well as not having the problem you described that also allows you to use other unused indices to store other attributes about the stack, e.g. a description like `x[-1] = "this is a stack of connection requests"`, which you couldn't do if you were relying on `length(x)` to tell you the index of the top of the stack.

Regards,

    Ed.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]