Path: csiph.com!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: Ed Morton Newsgroups: comp.lang.awk Subject: Re: Nitpicking the code (Was: Experiences with match() subexpressions?) Date: Mon, 14 Apr 2025 18:55:22 -0500 Organization: A noiseless patient Spider Lines: 64 Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Tue, 15 Apr 2025 01:55:22 +0200 (CEST) Injection-Info: dont-email.me; posting-host="0525a88dd16830ad096abe11874f7292"; logging-data="2538008"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/jLkTdJLnQ4YmdY3hWLR/E" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:HGPDYy+x60PpLYypCK5nK0o9djs= In-Reply-To: X-Antivirus-Status: Clean Content-Language: en-US X-Antivirus: Avast (VPS 250414-0, 4/13/2025), Outbound message Xref: csiph.com comp.lang.awk:9950 On 4/14/2025 1:53 PM, Janis Papanagnou wrote: > On 14.04.2025 20:20, Kenny McCormack wrote: >> In article , >> Ed Morton wrote: >> ... >>> data = "R=\"R=r1,R=r2\",R=r2,R=r3,E=e" >>> nf = patsplit(data, arr, /[RE]=([^,]*|"([^"]|"")*")/) >>> delete arr >>> for ( i in arr ) { >>> sub(/[^=]+=/, "", arr[i]) >>> } >> >> This can't be right, since if the sequence: >> delete arr >> for (i in arr) ... >> can't possibly do anything. I.e., the for statement will be a no-op, since >> the array is empty at that point. Yeah, remove that `delete arr`, it's not necessary since `patsplit()` will delete `arr` before populating it and `delete arr` in that location would break the code. >> >>> or any awk: >>> >>> data = "R=\"R=r1,R=r2\",R=r2,R=r3,E=e" >>> nf = 0 >>> delete arr >>> while ( match(data, /[RE]=([^,]*|"([^"]|"")*")/, a) ) { >>> arr[++nf] = substr(data, RSTART+2, RLENGTH-2) >>> data = substr(data, RSTART+RLENGTH) >>> } >> >> I believe "delete arr" (without an index, hence removing the entire array) >> is an "extension". I can't quite quote chapter and verse, but I note that >> "man mawk" explicitly mentions that mawk supports this syntax, thereby >> implying that it isn't "standard". Of course, gawk supports it as well. `delete arr` is defined by the current POSIX standard (https://pubs.opengroup.org/onlinepubs/9799919799/utilities/awk.html) as equivalent to `for (index in array) delete array[index]` but for years prior to that [almost?] every maintained awk supported `delete arr` anyway. >> So, if by "any awk", you mean "strictly standard", then, well, you can see >> where I am going with this. > > I seem to recall that a standard way to clear an array could be using > split("", arr) `split("", arr)` was the defacto "standard" way to delete an array's content without looping before `delete arr` was adopted by POSIX. In all seriousness if anyone is using an awk that doesn't support `delete arr` then they need to get a new awk as who knows what other features it might be lacking. Ed. > for example. To my taste it looks a bit clumsy, not as nice as using > 'delete', but well, whatever one prefers. > > Janis >